zoukankan      html  css  js  c++  java
  • 忍不住吐槽类模板、模板类、函数模板、模板函数

      最近在查资料,发现了一些blog上写"类模板、模板类、函数模板、模板函数的区别"之类的文章。一看之下,闭起眼睛想想,自己写了这么久C++,知道模板,知道函数,也知道类。如果单独问我,类模板或者模板类,我都认为是采用了模板的类。但不知道这"类模板、模板类、函数模板、模板函数"是什么东西。

      仔细看了下文章,忍不住吐槽了。其实就是采用模板的类叫类模板,实例化后叫模板类。采用模板的函数叫函数模板,实例化后叫模板函数。好吧,你赢了。知道模板的都会知道,模板实例化后会由编译器生成具体的类(函数)。但却不是所有程序员都会知道模板类、模板函数这么别扭的名字。换句话说,你上大街去问问,有人知道"模月饼"是什么不?因为所有月饼都是由月饼模做出来的,应该叫"模月饼"。

      所以,我认为这种概念还是不要用的好。一是很多书上没有说模板函数这种名词(很多C++的大作也没有),二是大家都认为实例化后就是普通的类、函数了,叫类、函数就好,如果真需要强调,叫实例化后的函数也容易理解。

      到网上查一下,发现还真有不少。其中stackoverflow也找到了相关的说法:

    Q: Is there a difference between a function template and a template function, or between a class template and a template class?
    
    A: The term "function template" refers to a kind of template. The term "template function" is sometimes used to mean the same thing,
    and sometimes to mean a function instantiated from a function template. This ambiguity is best avoided by using "function template"

    for the former and something like "function template instance" or "instance of a function template" for the latter. Note that a function
    template is not a function. The same distinction applies to "class template" versus "template class". Note that the 1998 C++ standard used the terms "template class" and "template function" in some places, but this was corrected in the 2003 version.

    http://stackoverflow.com/questions/1117755/what-is-the-difference-between-function-template-and-template-function

    http://womble.decadent.org.uk/c++/template-faq.html#phrase-order

    PS:刚毕业面试时被问到模板的特化,我说我不懂。然后说了半天,原来就是实例化,真是尴尬。

  • 相关阅读:
    不同storyboard间跳转
    WWDC2014:留给微软的时间不多了!
    IBOutletCollection 索引获取顺序问题
    iOS下单例模式实现(二)利用宏定义快速实现
    iOS下单例模式实现(一)(objective-c arc gcd)
    windows phone UI吐槽---跑偏了就再也跑不回来了
    iOS沙盒(SanBox)机制与文件操作
    转载:iOS静态库与动态库
    KVO与KVC理解
    iOS多线程编程的几种方式
  • 原文地址:https://www.cnblogs.com/coding-my-life/p/4199986.html
Copyright © 2011-2022 走看看