Krzysztof Stanisławek
Krzysztof Stanisławek

Reputation: 1277

Specializing only nested template

I have following template:

template<typename FirstParam>
struct First
{
    template<typename SecondParam>
    struct Second;
};

Example specialization:

template<typename T> class D {};

template<>
template<>
struct First<C1>::Second<C1::C2>
{
    typedef D<C1::C2> type;
};

This is case when both classes are specialized at the same time. But is there possible to specialize only second class?

Something like:

template<typename OuterParam>
template<>
struct Outer<OuterParam>::Inner<typename OuterParam::C2>
{
    typedef E<typename OuterParam::C2> type;
};

(Yes, I also need second param to be inner class of the first.)

Upvotes: 3

Views: 79

Answers (1)

T.C.
T.C.

Reputation: 137404

No. §14.7.3 [temp.expl.spec]/p16, emphasis added:

In an explicit specialization declaration for a member of a class template or a member template that appears in namespace scope, the member template and some of its enclosing class templates may remain unspecialized, except that the declaration shall not explicitly specialize a class member template if its enclosing class templates are not explicitly specialized as well.

You can use a "partial" specialization and std::is_same instead:

template<typename FirstParam>
struct First
{
    template<typename SecondParam, bool = std::is_same<typename FirstParam::C2,
                                                       SecondParam>::value>
    struct Second;

    template<class T>
    struct Second<T, true> { 
    };
};

Upvotes: 4

Related Questions