4

Syntax of Function Template

template <**class** T, ...>
    returntype functionname(arguments)
    {
           .....
           .....
     }

I have two Questions?

  1. Why the template parameter should be declared as a class type?(ie with the use of class keyword)
  2. When we declared it as a class type then what the thing the compiler will do?
4

5 回答 5

11

That's the usual confusion that arises from the usage of class in template arguments.

That class thing has nothing to do with classes; it merely says that the template accepts a type template argument (as opposed to integral1 template arguments), which can be any type, not only classes.

So, why did they choose class? Because they had to use a keyword that was surely not used in any C++ program and more or less "sounded good" - and class was ok, since it was already a reserved keyword in C++.

Notice that there's an alternative to class: the typename keyword. They are perfectly equivalent2, but typename in my opinion is much more clear, since the name just says "what follows is a type argument", without making you think that it must be a class.

Why both syntax are allowed? Because the typename keyword had do be introduced in the language later (when they noticed that it was necessary to add another keyword to disambiguate some declarations inside templates); then, it was "retrofitted" also for the template arguments declarations. This usage of the class keyword was kept for compatibility with programs/documentation written in the meantime.


  1. here I say "integral" for simplicity, obviously I mean non-type template parameters in general (C++11, §14.1 ¶4).
  2. There is no semantic difference between class and typename in a template-parameter.

    (C++11, §14.1 ¶2)

于 2012-04-22T14:01:21.057 回答
2

因为这是语言定义告诉您使用的词。在这种情况下,“T 类”表示“T 是某个类型的名称”,而不是“T 是某个类的名称”。

我相信理由在于不想再添加另一个保留字。

但是,该语言最终添加了另一个保留字:您可以等效地说“typename T”。

于 2012-04-22T14:02:17.923 回答
1

请注意,您可以将标准类型传递给template,而不仅仅是类:

template <class T, int N> class mysequence {..};

因此,class此处的关键字告诉编译器将 T 视为类。N被视为整数。

于 2012-04-22T14:02:50.420 回答
1

According to the Standard, there are two keywords : class and typename. You can use any one of them in a template definition. Both has same meaning: when you write class (or typename) in a template definition, it means the user of the template has to pass a type as template argument to the template; it doesn't mean anything more than that. If it is a function template, then the template argument may be deduced (in some cases) from the argument to the function.

于 2012-04-22T14:00:31.043 回答
0

(1) 为什么模板参数要声明为类类型?

完全正确。你也可以使用typename。:)
更重要的是,您也可以const将某些类型的对象声明为参数。例如

template<int I>  // <---- 'I' is not a class/typename
class A { ... };
...
A<3> obj;

(2) 当我们将它声明为类类型时,编译器会做什么?

其实不多
但是,当您调用它的对象时,编译器会检查该类型实际上是一个类型名称。例如

template<class T>
class A { ... };  // ok ... not much to check
...
A<int> obj1; // compiler checks if 'int' is a type ---> yes
A<3> obj2; // compiler checks if '3' is a type ---> no
于 2012-04-22T14:01:31.430 回答