When I use template partial specialization on a class with one template argument, I can specialize a method like this:
#include
template<
Editing since I cannot post comments yet (50 rep heh)...
Philippe, in response to your comment this morning, according to the standard you cannot partially specialize a member of a class template, you can only fully specialize them (whether it be a class template, a function, a function template, etc...). In your first example, you are fully specializing the member function foo. In your second example, you are partially specializing, reason why it will not compile. You can fully specialize it this way:
template< >
inline int Test< int, 2 >::foo()
{...}
Although Konrad's code snipet is perfectly legal, I'm not sure the reason provided as to why Philippe's code doesn't compile is correct. (Although, as Konrad mentionned, you cannot partially specialize a function template).
The issue at hand, in Philippe's code, is that we are declaring a class template and not a function template. Hence, a partial class template specialization declaration is needed for the partial specialization definition to be legal.
#include <cstdlib>
template< typename T, std::size_t Dim >
class Test
{
public:
int foo();
};
template < typename T >
class Test < T, 1 >
{
public:
int foo();
};
template< typename T, std::size_t Dim >
inline int Test< T, Dim >::foo()
{
return 0;
}
template< typename T >
inline int Test< T, 1 >::foo()
{
return 1;
}
int main()
{
Test< double, 2 > wTest2;
Test< int, 1 > wTest1;
wTest2.foo();
wTest1.foo();
return 0;
}
You cannot partially specialise functions – this includes member functions. You can only partially specialise the whole class:
template< typename T, std::size_t Dim >
class Test
{
public:
int foo()
{
return 0;
}
};
template< typename T >
class test< T, 1 >
{
public:
int foo()
{
return 1;
}
};
(I’ve defined the functions inline here; that of course isn’t necessary.)