Nested template only specialization
I have the following template:
template<typename FirstParam>
struct First
{
template<typename SecondParam>
struct Second;
};
Specialization example:
template<typename T> class D {};
template<>
template<>
struct First<C1>::Second<C1::C2>
{
typedef D<C1::C2> type;
};
This is the case when both classes specialize at the same time. But can only the second class be specialized?
Something like:
template<typename OuterParam>
template<>
struct Outer<OuterParam>::Inner<typename OuterParam::C2>
{
typedef E<typename OuterParam::C2> type;
};
(Yes, I also need a second parameter for the inner class of the first.)
source to share
Not. ยง14.7.3 [temp.expl.spec] / p16, emphasis added:
In an explicit description of a specialization for a class member, a template or member template that appears in the scope of a namespace, the member template and some of its enclosing class templates may remain unspecialized, except that the declaration must not explicitly specialize on the class member template unless its built-in class templates are also clearly not specialized.
Instead, you can use "partial" specialization and std::is_same
:
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> {
};
};
source to share