This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

template static member instantiation

I have a language question about instantiating templates with static

Here is a small test program:

  template <typename T> class gnu_obj_2
      static int my_static;

  template <typename T> int gnu_obj_2<T>::my_static = 117;

  // instantiate templates explicitly so their static members will exist
  template class gnu_obj_2 <int>;
  template class gnu_obj_2 <long>;

  int main ()
    gnu_obj_2 <int>  test_int;
    gnu_obj_2 <long> test_long;
    return 0;

gcc HEAD changed its behavior recently:

  gcc HEAD 2004-05-14
    the generated code has weak definitions for gnu_obj_2<int>::my_static
    and gnu_obj_2<long>::my_static.

  gcc HEAD 2004-05-24
    there are no definitions for gnu_obj_2<int>::my_static and

I ran across this issue when I ran the gdb test suite, which has a
larger program, gdb.cp/  gdb.cp/m-static.exp compiles the
test program and then prints the values of static template date members.

My platform is native i686-pc-linux-gnu.
My command line is "g++ -S -gstabs+".

My question is: should this sample program emit definitions for
the static template members gnu_obj_2<int>::my_static and
gnu_obj_2<long>::my_static?  Or do I have to add something
like "template int gnu_obj_2 <int>::my_static" to instantiate
each my_static?

Or, in other words, do I have to file a PR against gcc,
or do I have to fix my test program?

Looking at Stroustroup, C++PL, 3rd edition, section C.13.1 says: "Static
members must be separately defined and can be specialized." Section
C.13.10 says: "When a class template is explicitly instantiated, every
member function is also instantiated".  That kind of implies that the
test program needs an explicit instantiation for each my_static.  But
I'd like to get an expert opinion.


Michael C

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]