template static member instantiation
Michael Elizabeth Chastain
mec.gnu@mindspring.com
Fri May 28 13:46:00 GMT 2004
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
More information about the Gcc
mailing list