public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/23370] New: No error at -O1 for static const class member passed by reference
@ 2005-08-13  4:12 flash at pobox dot com
  2005-08-13  4:13 ` [Bug c++/23370] " flash at pobox dot com
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: flash at pobox dot com @ 2005-08-13  4:12 UTC (permalink / raw)
  To: gcc-bugs

The invalid code below gives an "undefined reference" linker error on GCC 4.0.1 at -O0, but not at -
O1; GCC 3.3.4 gives an error at both optimizations.  This may not be a bug, if the version 4 optimizer is 
being cleverer than the version 3 optimizer; but the failure to reject the invalid code might be 
considered a regression.


//By Stuart A. Malone, tweaked by Flash Sheridan

#include <stdio.h>
#include <stdint.h>

class Foo {
public:
     static const uint16_t kErrorCmd = 42;
};

static uint32_t TestCmd(const uint16_t & cmd)
{
     return (cmd == 42) ? 17 : 0;
}

int main()
{
     printf("%u", TestCmd(Foo::kErrorCmd) );
}

-----------

Here's the correct rejection:

368> /opt/gcc401chk/bin/g++ ../cpp/noerror/Llamagrahpics_static_constant_by_ref.cpp
/tmp/cc3Hoo7z.o(.text+0x47): In function `main':
: undefined reference to `Foo::kErrorCmd'
collect2: ld returned 1 exit status


Here's the full session for the failure to reject:
369> /opt/gcc401chk/bin/g++ -O1 -v ../cpp/noerror/Llamagrahpics_static_constant_by_ref.cpp
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../configure --enable-checking --prefix=/opt/gcc401chk --enable-languages=c,c+
+
Thread model: posix
gcc version 4.0.1
 /opt/gcc401chk/libexec/gcc/i686-pc-linux-gnu/4.0.1/cc1plus -quiet -v -D_GNU_SOURCE ../cpp/
noerror/Llamagrahpics_static_constant_by_ref.cpp -quiet -dumpbase 
Llamagrahpics_static_constant_by_ref.cpp -mtune=pentiumpro -auxbase 
Llamagrahpics_static_constant_by_ref -O1 -version -o /tmp/ccBQBEWy.s
ignoring nonexistent directory "/opt/gcc401chk/lib/gcc/i686-pc-linux-gnu/4.0.1/../../../../i686-pc-
linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /opt/gcc401chk/lib/gcc/i686-pc-linux-gnu/4.0.1/../../../../include/c++/4.0.1
 /opt/gcc401chk/lib/gcc/i686-pc-linux-gnu/4.0.1/../../../../include/c++/4.0.1/i686-pc-linux-gnu
 /opt/gcc401chk/lib/gcc/i686-pc-linux-gnu/4.0.1/../../../../include/c++/4.0.1/backward
 /usr/local/include
 /opt/gcc401chk/include
 /opt/gcc401chk/lib/gcc/i686-pc-linux-gnu/4.0.1/include
 /usr/include
End of search list.
GNU C++ version 4.0.1 (i686-pc-linux-gnu)
        compiled by GNU C version 3.3.4 (pre 3.3.5 20040809).
GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
 as -V -Qy -o /tmp/ccgiUdjh.o /tmp/ccBQBEWy.s
GNU assembler version 2.15.91.0.2 (i586-suse-linux) using BFD version 2.15.91.0.2 20040727 (SuSE 
Linux)
 /opt/gcc401chk/libexec/gcc/i686-pc-linux-gnu/4.0.1/collect2 --eh-frame-hdr -m elf_i386 -
dynamic-linker /lib/ld-linux.so.2 /usr/lib/crt1.o /usr/lib/crti.o /opt/gcc401chk/lib/gcc/i686-pc-
linux-gnu/4.0.1/crtbegin.o -L/opt/gcc401chk/lib/gcc/i686-pc-linux-gnu/4.0.1 -L/opt/gcc401chk/
lib/gcc/i686-pc-linux-gnu/4.0.1/../../.. /tmp/ccgiUdjh.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc 
/opt/gcc401chk/lib/gcc/i686-pc-linux-gnu/4.0.1/crtend.o /usr/lib/crtn.o
 flash@thrallis scripts 21:09:40
370>

-- 
           Summary: No error at -O1 for static const class member passed by
                    reference
           Product: gcc
           Version: 4.0.1
            Status: UNCONFIRMED
          Severity: minor
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: flash at pobox dot com
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


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


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

* [Bug c++/23370] No error at -O1 for static const class member passed by reference
  2005-08-13  4:12 [Bug c++/23370] New: No error at -O1 for static const class member passed by reference flash at pobox dot com
@ 2005-08-13  4:13 ` flash at pobox dot com
  2005-08-13  4:19 ` flash at pobox dot com
  2005-08-13  6:42 ` pinskia at gcc dot gnu dot org
  2 siblings, 0 replies; 4+ messages in thread
From: flash at pobox dot com @ 2005-08-13  4:13 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From flash at pobox dot com  2005-08-13 04:13 -------
Created an attachment (id=9489)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=9489&action=view)
Llamagrahpics_static_constant_by_ref.cpp


-- 


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


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

* [Bug c++/23370] No error at -O1 for static const class member passed by reference
  2005-08-13  4:12 [Bug c++/23370] New: No error at -O1 for static const class member passed by reference flash at pobox dot com
  2005-08-13  4:13 ` [Bug c++/23370] " flash at pobox dot com
@ 2005-08-13  4:19 ` flash at pobox dot com
  2005-08-13  6:42 ` pinskia at gcc dot gnu dot org
  2 siblings, 0 replies; 4+ messages in thread
From: flash at pobox dot com @ 2005-08-13  4:19 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From flash at pobox dot com  2005-08-13 04:18 -------
Here's the discussion in our developer forum about the validity of this code; the rejection was originally 
reported as a bug against our compiler.

At 4:34 PM -0500 2/16/04, Stuart A. Malone wrote:
>But when I compile this kind of code using the native compiler, the 
>linker complains that Foo::kVersionNumber is an undefined symbol. 
>If I try to mollify it by placing code in my .cpp file like this:
>
> const int16_t Foo::kVersionNumber = 3;
>
>The compiler complains that the symbol has been defined twice.
>
>So how do I win?

I checked a reference manual[1] last night, which said that the 
definition should be:

const int16_t Foo::kVersionNumber;

Keep the "= 3" in the class definition, like you did before.  All 
you're doing with the above line is telling the compiler and linker 
where the storage should be defined.

-- Keith Rollin
-- Development Tools engineer

[1] "The C Programming Language", Special Edition, p. 249, section 
10.4.6.2, "Member Constants", where it says "If (and only if) you use 
an initialized member in a way that requires it to be stored as an 
object in memory, the member must be (uniquely) defined somewhere. 
The initializer may not be repeated: <example>".


-- 


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


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

* [Bug c++/23370] No error at -O1 for static const class member passed by reference
  2005-08-13  4:12 [Bug c++/23370] New: No error at -O1 for static const class member passed by reference flash at pobox dot com
  2005-08-13  4:13 ` [Bug c++/23370] " flash at pobox dot com
  2005-08-13  4:19 ` flash at pobox dot com
@ 2005-08-13  6:42 ` pinskia at gcc dot gnu dot org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-08-13  6:42 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2005-08-13 06:42 -------
Since the static const can be "inlined" at higher level of optimizations which is allowed by the C++ 
standard, this is not a bug.

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


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


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

end of thread, other threads:[~2005-08-13  6:42 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-08-13  4:12 [Bug c++/23370] New: No error at -O1 for static const class member passed by reference flash at pobox dot com
2005-08-13  4:13 ` [Bug c++/23370] " flash at pobox dot com
2005-08-13  4:19 ` flash at pobox dot com
2005-08-13  6:42 ` pinskia at gcc dot gnu dot 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).