[Bug c++/49896] New: undefined reference to static const integral member whose address is not used, for some values of the constant

2011-07-29 Thread matthieu.imb...@ens-lyon.fr
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 test {
 protected:
  static const int version = 0x8000;
  //static const int version = 0x0fff;
 public:
  int getVersion();
};

template
int test::getVersion() {
  return version;
}

class dummy_class {};

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

<<< end of snippet <<<

compilation:

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

if the constant value is 0x0fff instead of 0x8000 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.


[Bug c++/49896] undefined reference to static const integral member whose address is not used, for some values of the constant

2011-07-29 Thread matthieu.imb...@ens-lyon.fr
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49896

--- Comment #2 from Matthieu Imbert  2011-07-29 
10:37:33 UTC ---
(In reply to comment #1)
> You lack a definition of test::version.

C++ standard section 9.4.2 states:

  If a 'static' data member is of 'const' integral or 'const' enumeral type,
  its declaration in the class definition can specify a 'constant-initializer'
  which shall be an integral constant expression (5.19).  In that case, the
  member can appear in integral constant expressions.  The member shall still
  be defined in a namespace scope if it is used in the program and the
  namespace scope definition shall not contain an 'initializer'.

The notion of "used" is defined in section 3.2:

  An object or non-overloaded function whose name appears as a potentially-
  evaluated expression is used unless it is an object that satisfies the
  requirements for appearing in a constant expression (5.19 [expr.const]) and
the
  lvalue-to-rvalue conversion (4.1 [conv.lval]) is immediately applied.

I am not a C++ expert but various sources (for example: stroustrup C++ faq:
http://www2.research.att.com/~bs/bs_faq2.html#in-class) seem to say that the
code snippet i sent is valid since the address of test::version is not
taken. Stroustrup says: "You can take the address of a static member if (and
only if) it has an out-of-class definition", which i interpret as: no need for
a definition if address not taken.

So in brief, i think there is no need to define test::version to be correct.


[Bug c++/49896] undefined reference to static const integral member whose address is not used, for some values of the constant

2011-07-29 Thread matthieu.imb...@ens-lyon.fr
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49896

Matthieu Imbert  changed:

   What|Removed |Added

 Status|RESOLVED|UNCONFIRMED
 Resolution|INVALID |

--- Comment #3 from Matthieu Imbert  2011-07-29 
12:49:46 UTC ---
forgot to change bug state