代码之家  ›  专栏  ›  技术社区  ›  Klaus

模板类中的静态模板变量

  •  -1
  • Klaus  · 技术社区  · 7 年前

    我想定义模板化类的静态模板变量。但我在这里找不到正确的语法:

    template < typename T>
    class X
    {
        public:
            T i;
            X(T _i): i{_i}{}
    
            operator T(){ return i; }
    }; 
    
     template < typename T2 >
     class Y
     {
         public:
             template <typename T>
                 static X<T> x_in_y;
     };
    
     // something like that, which currently do not compile     
     template< typename T2, typename T>
     X<T> Y<T2>::x_in_y<T>{9.9};
    
     int main()
     {
          std::cout << Y<int>::x_in_y<float> << std::endl;
     }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   David G    7 年前

    x_in_y 是模板中的模板,因此需要嵌套模板声明:

    template<typename T2>
    template<typename T>
    X<T> Y<T2>::x_in_y{9.9};