我有以下模板:
template<typename FirstParam>
struct First
{
template<typename SecondParam>
struct Second;
};
特化示例:
template<typename T> class D {};
template<>
template<>
struct First<C1>::Second<C1::C2>
{
typedef D<C1::C2> type;
};
这是两个类同时特化的情况。但是有可能只专二等吗?
类似于:
template<typename OuterParam>
template<>
struct Outer<OuterParam>::Inner<typename OuterParam::C2>
{
typedef E<typename OuterParam::C2> type;
};
(是的,我还需要第二个参数成为第一个参数的内部类。)
最佳答案
没有。 §14.7.3 [temp.expl.spec]/p16,重点补充:
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.
您可以使用“部分”特化和 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> {
};
};
关于c++ - 仅专门化嵌套模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26045191/