-1

I want to define the static template variable of a templated class. But I can't get the correct syntax here:

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;
 }
Klaus
  • 24,205
  • 7
  • 58
  • 113
  • Don't use static template variable member of template class with clang! https://godbolt.org/z/dCZq7I That makes Clang crash, or worst! – Oliv Dec 08 '18 at 23:04
  • @Oliv: Ubs! Thanks for the hint. That seems that both compilers are buggy here! – Klaus Dec 08 '18 at 23:09
  • No that is only a Clang bug , gcc does not have this one. – Oliv Dec 08 '18 at 23:13

1 Answers1

1

x_in_y is a template in a template so you need a nested template declaration:

template<typename T2>
template<typename T>
X<T> Y<T2>::x_in_y{9.9};
David G
  • 94,763
  • 41
  • 167
  • 253
  • g++ complains about "X Y::x_in_y{9.9};" but "X Y::x_in_y{9.9};" works?! – Klaus Dec 08 '18 at 22:24
  • You don't define a variable by saying int foo; but that's essentially what your first syntax tries to do. – xaxxon Dec 08 '18 at 22:47
  • @Klaus Get rid of the rightmost ``. I mistakenly had that in my answer too. – David G Dec 08 '18 at 22:49
  • Yep, already done ;) Can you tell in addition something about the partial specialization? I added some use cases to my question. Thanks! – Klaus Dec 08 '18 at 22:50
  • @Klaus You should put that as a separate question. – David G Dec 08 '18 at 22:58
  • @0x499602D2: Done! https://stackoverflow.com/questions/53687873/partial-specilization-of-static-template-var-in-template-class – Klaus Dec 08 '18 at 23:08