Initialize a static member of the template inner class

I have a problem with the syntax needed to initialize a static member in a class template. Here is the code (I tried to reduce it as much as I could):

template <typename T> struct A { template <typename T1> struct B { static T1 b; }; B<T> b; typedef B<T> BT; T val() { return bb; } }; template <typename T> T A<T>::BT::b; struct D { D() : d(0) {} int d; }; int main() { A<D> a; return a.val().d; } 

With g++ , I get the message:

 error: too few template-parameter-lists 

Any ideas on initializing b?

Please note that I would like to keep typedef as in my real code, B is more complicated than that.

+4
source share
1 answer

Change the definition of b to the following:

 template <typename T> template<typename T1> T1 A<T>::B<T1>::b; 

Note that typedef and B<T1> do not necessarily indicate the same type: although typedef relies on T , which is passed to b , B<T1> relies on the passed parameter of the T1 template. Therefore, you cannot use typedef here to specify a definition for b in B<T1> .

+6
source

Source: https://habr.com/ru/post/1301918/


All Articles