Static members in templates in egcs-1.0.1

Mark Mitchell mmitchell@usa.net
Wed Jan 14 04:17:00 GMT 1998


>>>>> "Erik" == Erik Corry <erik@arbat.com> writes:

    Erik>   template <class T> struct B { static T mycounter = 0; //
    Erik> with static - doesn't work with egcs //T mycounter; //
    Erik> without static - works with egcs void IncCounter()
    Erik> {mycounter++;} };
  
    Erik>   int main() { B<int> a; a.IncCounter(); return 0; }

As with static data members in non-templates, you need a definition of
the static data member at file-scope:

template <class T>
T B<T>::mycounter;

If you initialize the data member in-class, you should not initialize
it at file-scope.  (Currently, egcs doesn't issue an error message for
this, but I'm going to submit a patch to make sure it does real soon
now.)  

Note that in theory, you should not use in-class initialization with
non-const static data members; with -pedantic, g++ correctly says:

supernova% g++ -pedantic test.C
test.C:6: warning: ANSI C++ forbids in-class initialization of
non-const static member `mycounter'


-- 
Mark Mitchell		mmitchell@usa.net
Stanford University	http://www.stanford.edu




More information about the Gcc mailing list