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()
{}

      

+3


source to share


4 answers


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

      

+3


source


You cannot partially specialize a member function (and 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
};

      

+1


source


If you should 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();
};

      

0


source


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.

-2


source







All Articles