Partial Template Specialization

I have a script in which there is a template class

template<typename X, typename Y> class Foo { typedef Y::NestedType Bar; int A (Bar thing); void B(); int C(X that); // other stuff }; 

and then I would like method A () to have a different behavior when X is a given type (but B and C can remain unchanged, and the actual code actually has about 10 other methods, some of which are quite lengthy and subject to frequent tweaks. Therefore, I would prefer not to do full-fledged specialization and copy and paste the full implementation of the class)

I continued and wrote:

 template<typename T> int Foo<MyType, T>::A(Bar thing); 

but my compiler (clang 163.7.1) refused to even consider this as a specialized specialization.

Is there some kind of syntax error hidden in the way I wrote the code, or is this coding style invalid from C ++? Unfortunately, even if other compilers support the idiom, my company is stuck with clang.

Thanks for any help with this.

+7
source share
1 answer

Use overload

 template<typename X, typename Y> class Foo { // allows to wrap up the arguments template<typename, typename> struct Types { }; typedef Y::NestedType Bar; int A (Bar thing) { return AImpl(thing, Types<X, Y>()); } void B(); int C(X that); // other stuff private: template<typename X1, typename Y1> int AImpl(Bar thing, Types<X1, Y1>) { /* generic */ } template<typename Y1> int AImpl(Bar thing, Types<SpecificType, Y1>) { /* special */ } }; 

You cannot partially specialize a member of a class template. What you wrote would be the definition of a member function A partial specialization of the class template itself.

+7
source

All Articles