c++ - 仅专门化嵌套模板

标签 c++ templates template-specialization typename dependent-name

我有以下模板:

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/

相关文章:

c++ - 什么是 C++ 中用于分离 header /源的模板特化的可靠方法

C++ 类文件链接

c++ - 专门用于成员结构的 std 模板

python - django - 如何渲染我在网页中随处使用的模板?

c++ - 为什么 Curiously Recurring Template Pattern (CRTP) 有效

c++ - 在模板函数中使用 initializer_list

c++ - std::stoi 的基本参数

C++ 抽象基类的重新定义

c++ - 专门化 std::decay 有意义吗?

c++ - 无法捕获模板特化方法抛出的异常