Bug 23370 - No error at -O1 for static const class member passed by reference
Summary: No error at -O1 for static const class member passed by reference
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.0.1
: P2 minor
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
: 26597 (view as bug list)
Depends on:
Blocks:
 
Reported: 2005-08-13 04:12 UTC by Flash Sheridan
Modified: 2006-03-07 19:47 UTC (History)
2 users (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Llamagrahpics_static_constant_by_ref.cpp (353 bytes, text/plain)
2005-08-13 04:13 UTC, Flash Sheridan
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Flash Sheridan 2005-08-13 04:12:06 UTC
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>
Comment 1 Flash Sheridan 2005-08-13 04:13:12 UTC
Created attachment 9489 [details]
Llamagrahpics_static_constant_by_ref.cpp
Comment 2 Flash Sheridan 2005-08-13 04:18:57 UTC
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>".
Comment 3 Andrew Pinski 2005-08-13 06:42:15 UTC
Since the static const can be "inlined" at higher level of optimizations which is allowed by the C++ 
standard, this is not a bug.
Comment 4 Andrew Pinski 2006-03-07 19:47:37 UTC
*** Bug 26597 has been marked as a duplicate of this bug. ***