This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
template static member instantiation
- From: mec dot gnu at mindspring dot com (Michael Elizabeth Chastain)
- To: gcc at gcc dot gnu dot org
- Cc: carlton at kealia dot com
- Date: Fri, 28 May 2004 09:47:01 -0400 (EDT)
- Subject: template static member instantiation
I have a language question about instantiating templates with static
members.
Here is a small test program:
template <typename T> class gnu_obj_2
{
public:
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
gnu_obj_2<long>::my_static.
I ran across this issue when I ran the gdb test suite, which has a
larger program, gdb.cp/m-static.cc. 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.
Thanks,
Michael C