Can template classes have static members in C++ -


can template class in c++ have static members? since doesn't exist , imcomplete before used, possible?

yes. static member declared or defined inside template< … > class { … } block. if declared not defined, there must declaration provides definition of member.

template< typename t > class has_static {     // inline method definition: provides body of function.     static void meh() {}      // method declaration: definition body must appear later     static void fuh();      // definition of data member (i.e., declaration initializer)     // allowed const integral members     static int const guh = 3;      // declaration of data member, definition must appear later,     // if there no initializer.     static float pud; };  // provide definitions items not defined in class{} // these still go in header file  // function inline, because template template< typename t > void has_static<t>::fuh() {}  /* way templatize (non-function) object make static    data member of class. declaration takes form of template yet    defines global variable, bit special. */ template< typename t > float has_static<t>::pud = 1.5f; // initializer optional 

a separate static member created each parameterization of template. not possible have single member shared across all classes generated template. that, must define object outside template. partially-specialized traits class might that.


Comments