Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function template specialization format

What is the reason for the second brackets <> in the following function template:

template<> void doh::operator()<>(int i) 

This came up in SO question where it was suggested that there are brackets missing after operator(), however I could not find the explanation.

I understand the meaning if it was a type specialization (full specialization) of the form:

template< typename A > struct AA {}; template<> struct AA<int> {};         // hope this is correct, specialize for int 

However for function templates:

template< typename A > void f( A ); template< typename A > void f( A* ); // overload of the above for pointers template<> void f<int>(int);         // full specialization for int 

Where does this fit into this scenarion?:

template<> void doh::operator()<>(bool b) {} 

Example code that seems to work and does not give any warnings/error (gcc 3.3.3 used):

#include <iostream> using namespace std;  struct doh {     void operator()(bool b)     {         cout << "operator()(bool b)" << endl;     }      template< typename T > void operator()(T t)     {         cout << "template <typename T> void operator()(T t)" << endl;     } }; // note can't specialize inline, have to declare outside of the class body template<> void doh::operator()(int i) {     cout << "template <> void operator()(int i)" << endl; } template<> void doh::operator()(bool b) {     cout << "template <> void operator()(bool b)" << endl; }  int main() {     doh d;     int i;     bool b;     d(b);     d(i); } 

Output:

operator()(bool b) template <> void operator()(int i) 
like image 929
stefanB Avatar asked Jun 02 '09 02:06

stefanB


People also ask

What is function template specialization?

The act of creating a new definition of a function, class, or member of a class from a template declaration and one or more template arguments is called template instantiation. The definition created from a template instantiation is called a specialization.

What is the specialty of a template function give example?

Template in C++is a feature. We write code once and use it for any data type including user defined data types. For example, sort() can be written and used to sort any data type items. A class stack can be created that can be used as a stack of any data type.

What is explicit template specialization?

Explicit (full) specializationAllows customizing the template code for a given set of template arguments.

What does template <> mean in C++?

What Does Template Mean? A template is a C++ programming feature that permits function and class operations with generic types, which allows functionality with different data types without rewriting entire code blocks for each type.


1 Answers

I've looked it up, and found that it is specified by 14.5.2/2:

A local class shall not have member templates. Access control rules (clause 11) apply to member template names. A destructor shall not be a member template. A normal (non-template) member function with a given name and type and a member function template of the same name, which could be used to generate a specialization of the same type, can both be declared in a class. When both exist, a use of that name and type refers to the non-template member unless an explicit template argument list is supplied.

And it provides an example:

template <class T> struct A {     void f(int);     template <class T2> void f(T2); };  template <> void A<int>::f(int) { } // non-template member template <> template <> void A<int>::f<>(int) { } // template member  int main() {     A<char> ac;     ac.f(1); //non-template     ac.f(’c’); //template     ac.f<>(1); //template } 

Note that in Standard terms, specialization refers to the function you write using an explicit specialization and to the function generated using instantiation, in which case we have to do with a generated specialization. specialization does not only refer to functions you create using explicitly specializing a template, for which it is often only used.

Conclusion: GCC gets it wrong. Comeau, with which i also tested the code, gets it right and issues a diagnostic:

"ComeauTest.c", line 16: error: "void doh::operator()(bool)" is not an entity that can be explicitly specialized template<> void doh::operator()(bool i)

Note that it isn't complaining about the specialization of the template for int (only for bool), since it doesn't refer to the same name and type: The function type that specialization would have is void(int), which is distinct from the function type of the non-template member function, which is void(bool).

like image 183
Johannes Schaub - litb Avatar answered Sep 29 '22 21:09

Johannes Schaub - litb