Omegaman
Omegaman

Reputation: 2329

Specialization of a template to a nested class type

Is there anyway to specialize a trait template for a nested class? I've tried it in the three places noted below, each with the given error. I've seen questions regarding specializing nested template classes, but that's not what I'm trying to do here-- I'm trying to specialize a trait class that is used by the nested class.

The TraitUser class makes use of the definitions within the Trait as specialized to a specific type T. Perhaps most relevantly, it uses a trait member to initialize a base class.

template<T>
class TraitUser:public X<typename Trait<T>::Type>
{
  //Trait<T> gets used in here
};

//class A;
//class A::B;   <-incomplete type used in nested name

//template<>
//struct Trait<A::B>
//{};

class A
{
private:
    //class B;

    //template<>         <-explicit specialization at class scope
    //struct Trait<B>
    //{};

    class B:TraitUser<B>
        {};
};


//template<>        <- specialization after instantiation
//struct Trait<A::B>
//{};

It looks like the root of the trouble is not being able to forward declare a nested class and also not being able to define a specialization inside a class declaration.

I'm trying this under clang using C++11.

Upvotes: 4

Views: 675

Answers (1)

Casey
Casey

Reputation: 42544

There's some complicated declaration ordering here:

template <class T>
struct Trait;

template <class T>
struct X
{};

template<class T>
class TraitUser:public X<typename Trait<T>::Type>
{
  //Trait<T> gets used in here
};

class A
{
private:
    class B;
};


template<>
struct Trait<A::B>
{
    typedef int Type;
};

class A::B : public TraitUser<B>
{};

Upvotes: 3

Related Questions