public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [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  7:53 matthieu.imbert@ens-lyon.fr
  2011-07-29 10:06 ` [Bug c++/49896] " rguenth at gcc dot gnu.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: matthieu.imbert@ens-lyon.fr @ 2011-07-29  7:53 UTC (permalink / raw)
  To: gcc-bugs

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: unassigned@gcc.gnu.org
        ReportedBy: matthieu.imbert@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.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/49896] undefined reference to static const integral member whose address is not used, for some values of the constant
  2011-07-29  7:53 [Bug c++/49896] New: undefined reference to static const integral member whose address is not used, for some values of the constant matthieu.imbert@ens-lyon.fr
@ 2011-07-29 10:06 ` rguenth at gcc dot gnu.org
  2011-07-29 10:37 ` matthieu.imbert@ens-lyon.fr
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: rguenth at gcc dot gnu.org @ 2011-07-29 10:06 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Guenther <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID

--- Comment #1 from Richard Guenther <rguenth at gcc dot gnu.org> 2011-07-29 10:05:49 UTC ---
You lack a definition of test<C>::version.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/49896] undefined reference to static const integral member whose address is not used, for some values of the constant
  2011-07-29  7:53 [Bug c++/49896] New: undefined reference to static const integral member whose address is not used, for some values of the constant matthieu.imbert@ens-lyon.fr
  2011-07-29 10:06 ` [Bug c++/49896] " rguenth at gcc dot gnu.org
@ 2011-07-29 10:37 ` matthieu.imbert@ens-lyon.fr
  2011-07-29 12:50 ` matthieu.imbert@ens-lyon.fr
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: matthieu.imbert@ens-lyon.fr @ 2011-07-29 10:37 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Matthieu Imbert <matthieu.imbert@ens-lyon.fr> 2011-07-29 10:37:33 UTC ---
(In reply to comment #1)
> You lack a definition of test<C>::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<C>::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<C>::version to be correct.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/49896] undefined reference to static const integral member whose address is not used, for some values of the constant
  2011-07-29  7:53 [Bug c++/49896] New: undefined reference to static const integral member whose address is not used, for some values of the constant matthieu.imbert@ens-lyon.fr
  2011-07-29 10:06 ` [Bug c++/49896] " rguenth at gcc dot gnu.org
  2011-07-29 10:37 ` matthieu.imbert@ens-lyon.fr
@ 2011-07-29 12:50 ` matthieu.imbert@ens-lyon.fr
  2011-07-29 17:55 ` harald at gigawatt dot nl
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: matthieu.imbert@ens-lyon.fr @ 2011-07-29 12:50 UTC (permalink / raw)
  To: gcc-bugs

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

Matthieu Imbert <matthieu.imbert@ens-lyon.fr> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|INVALID                     |

--- Comment #3 from Matthieu Imbert <matthieu.imbert@ens-lyon.fr> 2011-07-29 12:49:46 UTC ---
forgot to change bug state


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/49896] undefined reference to static const integral member whose address is not used, for some values of the constant
  2011-07-29  7:53 [Bug c++/49896] New: undefined reference to static const integral member whose address is not used, for some values of the constant matthieu.imbert@ens-lyon.fr
                   ` (2 preceding siblings ...)
  2011-07-29 12:50 ` matthieu.imbert@ens-lyon.fr
@ 2011-07-29 17:55 ` harald at gigawatt dot nl
  2011-07-30 12:10 ` redi at gcc dot gnu.org
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: harald at gigawatt dot nl @ 2011-07-29 17:55 UTC (permalink / raw)
  To: gcc-bugs

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

Harald van Dijk <harald at gigawatt dot nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |harald at gigawatt dot nl

--- Comment #4 from Harald van Dijk <harald at gigawatt dot nl> 2011-07-29 17:55:21 UTC ---
0x80000000 is (possibly wrongly) rejected as an integer constant expression,
because it is not within int's range. It is accepted with an explicit cast.
With a slight modification, it triggers an ICE:

template<class C> 
class test {
 protected:
  static const int versionConst = 0x80000000;
  enum { versionEnum = versionConst }; 
 public:
  int getVersion();
};

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

class dummy_class {};

int main() {
  test<dummy_class> t; 
  return t.getVersion();
}
$ g++ -c test.cc
test.cc: In instantiation of ‘test<dummy_class>’:
test.cc:18:21:   instantiated from here
test.cc:5:8: error: the value of ‘test<dummy_class>::versionConst’ is not
usable in a constant expression
test.cc:5:8: internal compiler error: in non_const_var_error, at
cp/semantics.c:6852
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://gcc.gnu.org/bugs.html> for instructions.
$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-linux-gnu/4.6.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /var/tmp/pkgbuild/sys-devel/gcc-4.6.1/work/gcc-4.6.1/configure
--build=x86_64-pc-linux-gnu --host=x86_64-pc-linux-gnu --prefix=/usr
--enable-languages=c,c++,ada --enable-checking
Thread model: posix
gcc version 4.6.1 (GCC)


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/49896] undefined reference to static const integral member whose address is not used, for some values of the constant
  2011-07-29  7:53 [Bug c++/49896] New: undefined reference to static const integral member whose address is not used, for some values of the constant matthieu.imbert@ens-lyon.fr
                   ` (3 preceding siblings ...)
  2011-07-29 17:55 ` harald at gigawatt dot nl
@ 2011-07-30 12:10 ` redi at gcc dot gnu.org
  2011-07-30 14:59 ` harald at gigawatt dot nl
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: redi at gcc dot gnu.org @ 2011-07-30 12:10 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-07-30 12:09:28 UTC ---
The initializer results in integer overflow which is undefined behaviour, so if
G++ accepts it but requires the variable to be defined then that is a
reasonable result.

The modified example shouldn't cause an ICE, that's a regression, but it seems
to be fixed in 4.7 already


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/49896] undefined reference to static const integral member whose address is not used, for some values of the constant
  2011-07-29  7:53 [Bug c++/49896] New: undefined reference to static const integral member whose address is not used, for some values of the constant matthieu.imbert@ens-lyon.fr
                   ` (4 preceding siblings ...)
  2011-07-30 12:10 ` redi at gcc dot gnu.org
@ 2011-07-30 14:59 ` harald at gigawatt dot nl
  2011-10-09 15:23 ` paolo.carlini at oracle dot com
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: harald at gigawatt dot nl @ 2011-07-30 14:59 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Harald van Dijk <harald at gigawatt dot nl> 2011-07-30 14:59:09 UTC ---
> The initializer results in integer overflow which is undefined behaviour

No, it doesn't result in integer overflow. 0x80000000 is a constant of type
unsigned int, which is implicitly converted to the desired type of int.
Converting an out-of-range integer value to a signed integer type is
implementation-defined which has been defined in GCC in a sensible way,
documented at
<http://gcc.gnu.org/onlinedocs/gcc-4.6.1/gcc/Integers-implementation.html>.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/49896] undefined reference to static const integral member whose address is not used, for some values of the constant
  2011-07-29  7:53 [Bug c++/49896] New: undefined reference to static const integral member whose address is not used, for some values of the constant matthieu.imbert@ens-lyon.fr
                   ` (5 preceding siblings ...)
  2011-07-30 14:59 ` harald at gigawatt dot nl
@ 2011-10-09 15:23 ` paolo.carlini at oracle dot com
  2011-10-09 21:07 ` jason at gcc dot gnu.org
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: paolo.carlini at oracle dot com @ 2011-10-09 15:23 UTC (permalink / raw)
  To: gcc-bugs

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

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jason at gcc dot gnu.org

--- Comment #7 from Paolo Carlini <paolo.carlini at oracle dot com> 2011-10-09 15:22:48 UTC ---
Jason, I guess we need a final word about the triage of this.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/49896] undefined reference to static const integral member whose address is not used, for some values of the constant
  2011-07-29  7:53 [Bug c++/49896] New: undefined reference to static const integral member whose address is not used, for some values of the constant matthieu.imbert@ens-lyon.fr
                   ` (6 preceding siblings ...)
  2011-10-09 15:23 ` paolo.carlini at oracle dot com
@ 2011-10-09 21:07 ` jason at gcc dot gnu.org
  2011-10-11 17:54 ` jason at gcc dot gnu.org
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: jason at gcc dot gnu.org @ 2011-10-09 21:07 UTC (permalink / raw)
  To: gcc-bugs

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

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
   Last reconfirmed|                            |2011-10-09
         AssignedTo|unassigned at gcc dot       |jason at gcc dot gnu.org
                   |gnu.org                     |
     Ever Confirmed|0                           |1

--- Comment #8 from Jason Merrill <jason at gcc dot gnu.org> 2011-10-09 21:06:34 UTC ---
(In reply to comment #6)

Right.  I was surprised by this, but conversion of out-of-range values between
integral types is treated differently in the standard from overflow in
arithmetic operations.

4.7:

If the destination type is signed, the value is unchanged if it can be
represented in the destination type (and bit-field width); otherwise, the value
is implementation-defined.

So, the testcase is OK.  I think this bug is actually closely related to 49855,
as the fix for that bug also fixes this one for me.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/49896] undefined reference to static const integral member whose address is not used, for some values of the constant
  2011-07-29  7:53 [Bug c++/49896] New: undefined reference to static const integral member whose address is not used, for some values of the constant matthieu.imbert@ens-lyon.fr
                   ` (7 preceding siblings ...)
  2011-10-09 21:07 ` jason at gcc dot gnu.org
@ 2011-10-11 17:54 ` jason at gcc dot gnu.org
  2011-10-11 18:19 ` jason at gcc dot gnu.org
  2011-10-11 18:22 ` jason at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: jason at gcc dot gnu.org @ 2011-10-11 17:54 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from Jason Merrill <jason at gcc dot gnu.org> 2011-10-11 17:53:20 UTC ---
Author: jason
Date: Tue Oct 11 17:53:07 2011
New Revision: 179813

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=179813
Log:
    PR c++/49855
    PR c++/49896
    * cp-tree.def (IMPLICIT_CONV_EXPR): New.
    * call.c (perform_implicit_conversion_flags): Build it
    instead of NOP_EXPR.
    * cp-objcp-common.c (cp_common_init_ts): It's typed.
    * cxx-pretty-print.c (pp_cxx_cast_expression): Handle it.
    (pp_cxx_expression): Likewise.
    * error.c (dump_expr): Likewise.
    * semantics.c (potential_constant_expression_1): Likewise.
    * tree.c (cp_tree_equal): Likewise.
    (cp_walk_subtrees): Likewise.
    * pt.c (iterative_hash_template_arg): Likewise.
    (for_each_template_parm_r): Likewise.
    (type_dependent_expression_p): Likewise.
    (tsubst_copy, tsubst_copy_and_build): Handle IMPLICIT_CONV_EXPR
    and CONVERT_EXPR.
    * cp-tree.h (IMPLICIT_CONV_EXPR_DIRECT_INIT): New.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-template3.C
    trunk/gcc/testsuite/g++.dg/template/constant1.C
    trunk/gcc/testsuite/g++.dg/template/constant2.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/call.c
    trunk/gcc/cp/cp-objcp-common.c
    trunk/gcc/cp/cp-tree.def
    trunk/gcc/cp/cp-tree.h
    trunk/gcc/cp/cxx-pretty-print.c
    trunk/gcc/cp/error.c
    trunk/gcc/cp/pt.c
    trunk/gcc/cp/semantics.c
    trunk/gcc/cp/tree.c
    trunk/gcc/testsuite/ChangeLog


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/49896] undefined reference to static const integral member whose address is not used, for some values of the constant
  2011-07-29  7:53 [Bug c++/49896] New: undefined reference to static const integral member whose address is not used, for some values of the constant matthieu.imbert@ens-lyon.fr
                   ` (8 preceding siblings ...)
  2011-10-11 17:54 ` jason at gcc dot gnu.org
@ 2011-10-11 18:19 ` jason at gcc dot gnu.org
  2011-10-11 18:22 ` jason at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: jason at gcc dot gnu.org @ 2011-10-11 18:19 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #10 from Jason Merrill <jason at gcc dot gnu.org> 2011-10-11 18:18:35 UTC ---
Author: jason
Date: Tue Oct 11 18:18:25 2011
New Revision: 179815

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=179815
Log:
    PR c++/49855
    PR c++/49896
    * call.c (perform_implicit_conversion_flags): Do perform
    scalar conversions in templates.
    * pt.c (tsubst_copy, tsubst_copy_and_build): Handle CONVERT_EXPR.

Added:
    branches/gcc-4_6-branch/gcc/testsuite/g++.dg/template/constant1.C
    branches/gcc-4_6-branch/gcc/testsuite/g++.dg/template/constant2.C
Modified:
    branches/gcc-4_6-branch/gcc/cp/ChangeLog
    branches/gcc-4_6-branch/gcc/cp/call.c
    branches/gcc-4_6-branch/gcc/cp/pt.c
    branches/gcc-4_6-branch/gcc/testsuite/ChangeLog


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c++/49896] undefined reference to static const integral member whose address is not used, for some values of the constant
  2011-07-29  7:53 [Bug c++/49896] New: undefined reference to static const integral member whose address is not used, for some values of the constant matthieu.imbert@ens-lyon.fr
                   ` (9 preceding siblings ...)
  2011-10-11 18:19 ` jason at gcc dot gnu.org
@ 2011-10-11 18:22 ` jason at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: jason at gcc dot gnu.org @ 2011-10-11 18:22 UTC (permalink / raw)
  To: gcc-bugs

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

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED
   Target Milestone|---                         |4.6.2

--- Comment #11 from Jason Merrill <jason at gcc dot gnu.org> 2011-10-11 18:20:52 UTC ---
Fixed for 4.6.2.


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2011-10-11 18:22 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-29  7:53 [Bug c++/49896] New: undefined reference to static const integral member whose address is not used, for some values of the constant matthieu.imbert@ens-lyon.fr
2011-07-29 10:06 ` [Bug c++/49896] " rguenth at gcc dot gnu.org
2011-07-29 10:37 ` matthieu.imbert@ens-lyon.fr
2011-07-29 12:50 ` matthieu.imbert@ens-lyon.fr
2011-07-29 17:55 ` harald at gigawatt dot nl
2011-07-30 12:10 ` redi at gcc dot gnu.org
2011-07-30 14:59 ` harald at gigawatt dot nl
2011-10-09 15:23 ` paolo.carlini at oracle dot com
2011-10-09 21:07 ` jason at gcc dot gnu.org
2011-10-11 17:54 ` jason at gcc dot gnu.org
2011-10-11 18:19 ` jason at gcc dot gnu.org
2011-10-11 18:22 ` jason at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).