regnirpsj
regnirpsj

Reputation: 197

partial template member specialization

Given 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 the right magic to substitute the ??? below

template <???>
void
A<B<T>>::f()
{}

Upvotes: 3

Views: 70

Answers (4)

vsoftco
vsoftco

Reputation: 56547

You cannot partially specialize a member function (nor in fact any function). You need to partially specialize the whole class:

template<typename T>
class A<B<T>>
{
    // implement member functions for this specialization here
};

Upvotes: 1

R Sahu
R Sahu

Reputation: 206557

If you must have:

template <typename T>
void A<B<typename T>>::f() {}

then your only choice is to partially specialize A.

template <typename T> class A<B<T>>
{
   public:
      void f();
};

Upvotes: 0

Barry
Barry

Reputation: 302643

You can have an explicit specialization, from [temp.expl.spec]:

An explicit specialization of any of the following:
— ...
— member function of a class template
— ...
can be declared by a declaration introduced by template<>

That is:

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 would 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>> ...
};

Upvotes: 3

zmbq
zmbq

Reputation: 39013

C++11 has Alias Templates, allowing you do do something like:

template<T>
using AB = A<B<T>>;

Then you can refer to AB<T> instead of A<B<T>>.

Unfortunately, you can't use that for specialization..

So seems to me the answer to your question is: You can't do that, but it's a shame.

Upvotes: -2

Related Questions