Partial Template Specialization
Considering the following definitions:
template <typename T>
class A {
public:
void f();
};
template <typename T>
void
A<T>::f()
{}
template <typename T>
class B {};
How would I partially specialize A<B<T>>::f
, i.e. f
for some B<T>
? I'm basically looking for a suitable magic to substitute ???
below
template <???>
void
A<B<T>>::f()
{}
source to share
You can have an explicit specialization starting at [temp.expl.spec]:
Explicit specialization of any of the following:
-...
- member function of class template
-...
can be declared by declaration injectedtemplate<>
I.e:
template <>
void A<B<int>>::f() {
std::cout << "B\n";
}
But you cannot have a partial specialization of a member function of a class template. You will have to partially specialize the entire class:
template <typename T>
class A<B<T>> {
public:
void f() {
std::cout << "B\n";
}
// ... all other members you want in A<B<T>> ...
};
source to share
C ++ 11 has Alias Templates , allowing you to do something like:
template<T>
using AB = A<B<T>>;
Then you can link to AB<T>
instead A<B<T>>
.
Unfortunately, you cannot use this for specialization. ...
So, it seems to me that the answer to your question is: you cannot do this, but it's a shame.
source to share