[Bug c++/15935] New: Defining static [const] member variables of template class

Rainer dot Bensch at rsd dot rohde-schwarz dot com gcc-bugzilla@gcc.gnu.org
Fri Jun 11 08:49:00 GMT 2004


static [const] member variables of template classes, std::basic_string for 
instance, are not always emitted by the compiler.

//--- with gcc 3.2 PowerPC
//--- as tge.cpp:

#undef  _GLIBCPP_NO_TEMPLATE_EXPORT
#define _GLIBCPP_NO_TEMPLATE_EXPORT 1

#include <string>
#include <iostream>

namespace other
{

struct my_char_traits : public std::char_traits<char> {};

typedef std::basic_string<char, my_char_traits> my_string;

}


using namespace std;
using namespace other;

int main (int, const char*[])
{
   my_string x ("it works!");
   cout << x.c_str() << endl;

   return 0;
}

//--------------------------------------------------------- 
//---compiled with:
// -c -frtti -fno-gnu-keywords -msoft-float -fmerge-all-constants -O2
// -fno-inline-functions -fno-implement-inlines -Winline -Wall
// -Woverloaded-virtual -Wsynth -Wreturn-type -Wno-ctor-dtor-privacy -Wmultichar
// -Wfloat-equal -fno-weak -Wno-parentheses -g
//---------------------------------------------------------
//---linked with:
// --warn-once -nostdlib -M --cref -Map ...map -o ...elf -T ...lcf crt0.o 
// -(TEST_2.a libgcc.a libm.a libstdc++.a libsupc++.a libc.a ... -)

//---link errors:
// TEST_2.a(tge.o): In function `main':
// /cygdrive/include/c++/3.2/bits/char_traits.h:119: 
// undefined reference to `std::basic_string<char, other::my_char_traits, 
std::allocator<char> >::_Rep::_S_terminal'

// TEST_2.a(tge.o): In function `std::basic_string<char, other::my_char_traits, 
std::allocator<char> >::_Rep::_S_create(unsigned, std::allocator<char> const&)':
// /cygdrive/include/c++/3.2/bits/basic_string.tcc:388: 
// undefined reference to `std::basic_string<char, other::my_char_traits, 
std::allocator<char> >::_Rep::_S_max_size'

//--------------------------------------------------------- 

// 1) The templates defining both variables was seen by the compiler before 
//    instantiation of class my_string.
// 2) The templates work for std::string (if not otherwise resolved from a 
library)
// 3) Such templates defining static const variables declared in template 
classes
//    usually work fine. Must have something to do with mixing namespaces in
//    template parameters or declaring the class instance in another namespace,
//    I guess.
// 4) Workaround (neither portable nor resistant against stl changes):

// enable workaround
#if 0

// either one of the following works
#if 0
    namespace other
#else
    namespace std
#endif
    {

    const char   basic_string<char, other::my_char_traits>::_Rep::_S_terminal = 
char();
    const size_t basic_string<char, other::my_char_traits>::_Rep::_S_max_size = 
       (((basic_string<char, other::my_char_traits>::npos 
       - sizeof(basic_string<char, other::my_char_traits>::_Rep))/sizeof
(char)) - 1) / 4;

    }
#endif

-- 
           Summary: Defining static [const] member variables of template
                    class
           Product: gcc
           Version: 3.2
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: Rainer dot Bensch at rsd dot rohde-schwarz dot com
                CC: gcc-bugs at gcc dot gnu dot org


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



More information about the Gcc-bugs mailing list