代码之家  ›  专栏  ›  技术社区  ›  Aviv Cohn

如何根据不同的专业化设置内部模板类的特定成员?

  •  1
  • Aviv Cohn  · 技术社区  · 6 年前

    对于专门化外部非模板化类的内部模板化类,我知道以下是可能的(取自另一个SO问题):

    struct X
    {
        template <typename T>
        class Y
        {};
     };
    template<>
    class X::Y<double>{
    };
    

    但是,如果我只想设置内部类的专门化成员呢?有可能吗?以下尝试无法编译:

    struct C {
    
        template <typename T>
        struct Inner {
            static const int x;
        };
    };
    
    C::Inner<int>::x = 30;
    C::Inner<double>::x = 40;
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   geza    6 年前

    template <>
    const int C::Inner<int>::x = 30;
    
    template <>
    const int C::Inner<double>::x = 40;