Explicit specialization of nested class template in class template

812 views Asked by At

This is the minimal code that I can't fix:

template<typename T>
class A {
    template<typename S>
    class B{

    };
    template<>
    class B<int> {

    };
};

when I compile I get

error: explicit specialization in non-namespace scope 'class A<T>'

What am I doing wrong? How can I fix this?

EDIT: I've seen some other answers, but from what they suggested, I should do this:

template<typename T>
class A {
    template<typename S>
    class B{

    };
};
template<typename T>
class A<T>::B<int> {

};

But that doesn't work either...

1

There are 1 answers

0
justin On BEST ANSWER

Unfortunately, this partial specialization is invalid C++.

You may see the form in your example in the wild because MSC supported it as an extension.

Here's an approach which demonstrates how you can remove the dependence of the parent class:

namespace Imp {
template <typename S> class B {};
template <> class B<int> {};
}

template <typename T> class A {
public:
  template <typename S> using B = Imp::B<S>;
};