Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ template specialization of function: "illegal use of explicit template arguments"

The following template specialization code:

template<typename T1, typename T2>
void spec1()
{

}

Test case 1:

template< typename T1> //compile error
void spec1<int>()
{

}

Test case 2:

template< typename T2> //compile error
void spec1<int>()
{

}

generates the following compilation error:

error C2768: 'spec1' : illegal use of explicit template arguments

Does anyone know why?

like image 701
jameszhao00 Avatar asked Sep 12 '09 22:09

jameszhao00


1 Answers

Function templates cannot be partially specialised, only fully, i.e. like that:

template<>
void spec1<char, int>()
{

}

For why function templates cannot be partially specialised, you may want to read this.

When you specialise partially (only possible for classes), you'd have to do it like that:

template <typename T1>
class class1<T1, int>
{

};

so you have to list T1 again.

The way your specialisations are written, they would be ambiguous for spec1<int, int>.

like image 105
Rüdiger Hanke Avatar answered Oct 17 '22 00:10

Rüdiger Hanke