Reputation: 678
Environment: Microsoft Visual Studio 2010
Coding standard: C++0x compatible
I have a class template
template <typename T1, int I>
class A
{
public template <typename T2> void f(T2 x);
/*...*/
};
template <typename T1, int I>
template <typename T2>
void A<T1, I>::f(T2 x)
{
/*...*/
}
and partial specialization of above class
template <int I>
class A<char, I>
{
public template <typename T2> void f(T2 x);
/*...*/
};
Then can I specialize member function in the partially specialized class like below?
template <int I>
template <>
void A<char, I>::f<double>(double x)
{
}
Thanks!
NB: I'm not working on it but thinking if it's applicable or not. Easy rating if you know about the rule.
Upvotes: 1
Views: 401
Reputation: 507155
This is invalid because you cannot explicitly specialize a member function without also giving fixed template arguments to any enclosing class template.
Not C++11 compatible, but working on MSVC
The Microsoft compiler has an extension that allows to declare explicit specializations within class templates though. Even though I have never tried it, chances are good that it will accept the following non-standard code
template <int I>
class A<char, I>
{
public:
template <typename T2> void f(T2 x);
template<> void f<double>(double x) {
}
/*...*/
};
Update: Clang compiles this and reports
// clang++ -fms-extensions main1.cpp
main1.cpp:10:21: warning: explicit specialization of 'f' within class scope is a
Microsoft extension [-Wmicrosoft]
template<> void f<double>(double x) {
^
C++11/C++03 compatible
The way here is overloading instead of specialization
template <int I>
class A<char, I>
{
public:
template <typename T2> void f(T2 x);
void f(double x) {
}
/*...*/
};
Upvotes: 1