Suppose you have:
template<class T>
class A {
template<class T1>
void foo(const T1& t1) {}
//
// Lots of other definitions (all templated)
//
};
and you would like to specialize foo(const T1&) but only for a specialized A<bool>. Like this:
template<>
class A<bool> {
template<class T1>
void foo(const T1& t1) {
// Code for specialized A<boo>::foo
}
//
// Repeating the former definitions, how to avoid this ??
//
};
But to get this to work I have to duplicate all the code that is defined in the class template class A and include it again in class A<bool>.
I tried to define only the member specialization:
template<>
void A<bool>::template<class T1> foo(const T1&) {}
Neither does this work:
template <class T1> void A<bool>::foo(const T1&) {}
But the compiler doesn’t like it.
What’s the way to deal with this code duplication?
Syntax? See this answer. Try:
You certainly don’t need to copy the whole class template.