Reputation: 67
Can someone please explain me why is this not compiling...the specialized function of a specialized class ??? In the specialized version of the templated class the specialized function is not compiling.
#include<iostream>
using namespace std;
//Default template class
template<typename T>
class X
{
public:
void func(T t) const;
};
template<typename T>
void X<T>::func(T b) const
{
cout << endl << "Default Version" << endl;
}
//Specialized version
template<>
class X<int>
{
public:
void func(int y) const;
};
template<>
void X<int>::func(int y)
{
cout << endl << "Int Version" << endl;
}
int main()
{
return 0;
}
Upvotes: 1
Views: 137
Reputation: 126432
An explicit specialization of a class template is a concrete class, not a template, so you can (or rather, should) just write:
// template<>
// ^^^^^^^^^^
// You should not write this
void X<int>::func(int y) const
// ^^^^^
// And do not forget this!
{
cout << endl << "Int Version" << endl;
}
Thus leaving out the template<>
part.
Also, mind the fact that your func()
function is const
-qualified in the declaration - so you have to use the const
qualifier even in the defintion.
Here is a live example.
Upvotes: 3