Re: member template specialization - why not?
On 2011-01-20 13:04, Helmut Jarausch wrote:
Hi,
would anybody please explain to me why even the upcoming new C++ standard
seems to forbid the specialization of a member template like
// compile with g++ -std=c++0x
#include<iostream>
using std::cout; using std::endl;
struct C {
int Id;
C(int i) : Id(i) {}
This constructor is not used below and the member Id has no meaning in
this example. I assume that you just have declared a default constructor
as shown below.
template<typename T>
T operator()(const T& P) {
cout<< "templated version of fct-call-op\n";
return P;
}
template<>
int operator()(int Q) {
cout<< "specialized version of fct-call-op\n";
return Q;
}
Even if a specialization where allowed here, this is no valid
specialization, because the argument type is not matching correctly (you
need 'const int&' instead of 'int').
};
int main() {
C Test;
cout<< Test(3.14)<< " "<< Test(1)<< endl;
}
I have been think this restriction was related to the impossibility to
partially specialize a function template in the old standard.
This constraint is unrelated to your problem. The only (arguably
annoying) constraint in your example is, that you cannot declare such a
specialization within the class definition. But it is OK to define the
specialization out-of-class:
struct C {
C() {}
template <typename T>
T operator()(const T& P) {
cout << "templated version of fct-call-op\n";
return P;
}
};
template<>
inline int C::operator()(const int& Q) {
cout << "specialized version of fct-call-op\n";
return Q;
}
HTH & Greetings from Bremen,
Daniel Kr??gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]