You can't do it because the enclosing class isn't specialized. You can't partially or fully specialize a nested function or class unless the containing class is fully specialized. It is unfortunate, but the logic for this is sound. Do not even try it. You will only waste time. It can't work. (A compiler that does allow it is broken.)
You can, usually, simply overload the function. I would suggest you always mark such a function as 'inline' so the source can stay in the header with the source for the template. (You may get linker errors if you don't.) That isn't mandatory.
You can always specialize the interface and the mechanic of a nested function or class. The method, the technique, is ugly. It is also occasionally necessary if you employ a lot of sorcery. Put the technique in your bag of tricks for those occasions, but do try not to overuse it.
This is ugly. I'm kind of busy. It will show you some of what I'm talking about.
Soma
Code:
#include <iostream>
using namespace std;
template <class T> class I1;
template <class T, class U> struct helper
{
static I1<U> go(I1<T> & f);
};
template <class T> class I1
{
public:
T i, j;
I1(T a = 0, T b = 0): i(a), j(b){}
~I1(){}
template <class U> operator I1<U>()
{
return helper<T, U>::go(*this);
}
};
template <class T, class U> I1<U> helper<T, U>::go(I1<T> & f)
{
return I1 <U> (U(f.i), U(f.j));
}
template <class T> struct helper<T, char>
{
static I1<char> go(I1<T> & f);
};
template <class T> I1<char> helper<T, char>::go(I1<T> & f)
{
cout << "char specialisation" << endl;
return I1 <char> (char(f.i), char(f.j));
}
int main()
{
I1 <int> var2(97, 99);
cout << var2.i << ", " << var2.j << endl;
I1 <char> var3(var2);
cout << var3.i << ", " << var3.j << endl;
return 0;
}