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

           Summary: undefined reference to static const integral member
                    whose address is not used, for some values of the
                    constant
           Product: gcc
           Version: 4.6.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: matthieu.imb...@ens-lyon.fr


A static const int member is declared and initialized in a template class.
Its value (not its address) is used in a method of the class.
If the value of the constant has its most significant bit set (int is 32 bits),
then there is an undefined reference to the member at link.

Here is a code snippet to reproduce this:

>>> snippet test.cpp >>>

template<class C>
class test {
 protected:
  static const int version = 0x80000000;
  //static const int version = 0x0fffffff;
 public:
  int getVersion();
};

template<class C>
int test<C>::getVersion() {
  return version;
}

class dummy_class {};

int main() {
  test<dummy_class> t;
  return t.getVersion();
}

<<< end of snippet <<<

compilation:

$ gcc -Wall -Wextra -Wstrict-aliasing test2.cpp
/tmp/cc4duaE3.o: In function `test<dummy_class>::getVersion()':
test2.cpp:(.text._ZN4testI11dummy_classE10getVersionEv[test<dummy_class>::getVersion()]+0xa):
undefined reference to `test<dummy_class>::version'
collect2: ld returned 1 exit status

if the constant value is 0x0fffffff instead of 0x80000000 it compiles fine.

i get this issue on this version of gcc (the default version currently on
debian testing):

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.6.1/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.6.1-4'
--with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++,go --prefix=/usr
--program-suffix=-4.6 --enable-shared --enable-multiarch
--with-multiarch-defaults=x86_64-linux-gnu --enable-linker-build-id
--with-system-zlib --libexecdir=/usr/lib/x86_64-linux-gnu
--without-included-gettext --enable-threads=posix
--with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib/x86_64-linux-gnu
--enable-nls --enable-clocale=gnu --enable-libstdcxx-debug
--enable-libstdcxx-time=yes --enable-plugin --enable-objc-gc
--with-arch-32=i586 --with-tune=generic --enable-checking=release
--build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.6.1 (Debian 4.6.1-4)

I checked with gcc 4.5.3-3 and i don't have the link error.

Reply via email to