This is the mail archive of the gcc-bugs@gcc.gnu.org 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]

[Bug c++/14761] New: Fails to link when static member variable of the class template is defined with the default value


In the code below, we define a template class with static member variable, then
define that static member variable with a given value or without it, then try to
make use of that static member variable.

When we give the value for the variable, it works. When we leave it out without
value, it fails to link, complaining that it doesn't find the static variable.

The problem shows up in all versions of G++ I tried (2.95.4, 3.2, 3.3, 3.4 pre).

The code: 

test.cc
------------------------------------

#include <iostream>

// This template class holds static variable

template< class T >
class StaticHolder
{
public:

  static T staticMember;
};


// Define static member
template< class T > T StaticHolder< T > :: staticMember;


// Specialize static member for int

#ifdef BUG

// Doesn't work if no value is given
template<> int StaticHolder< int > :: staticMember;

#else

// This way it works
template<> int StaticHolder< int > :: staticMember = 0;

#endif


// Explicitly instantiate static member

/*
  Don't know if it is needed or not.
  2.95 doesn't like this, all 3.x think it's ok.
  It can be commented anyway as it doesn't seem to change anything.
*/
template int StaticHolder< int > :: staticMember;

// Explicitly instantiate the whole template for int, though it is not needed.
// It doesn't help anyway.
template class StaticHolder< int >;

// This code makes use of the static variable

int main()
{
  std::cout << StaticHolder< int > :: staticMember << std::endl;
}
------------------------------------

This way it works:

$ g++-3.3 -pedantic -Wall test.cc

This way it doesn't:

$ g++-3.3 -pedantic -Wall test.cc -DBUG

/tmp/cccuhj3Y.o(.text+0x11): In function `main':
: undefined reference to `StaticHolder<int>::staticMember'
collect2: ld returned 1 exit status

-- 
           Summary: Fails to link when static member variable of the class
                    template is defined with the default value
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: ikm at pisem dot net
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14761


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