public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/44673]  New: static const variable works in if/else, fails at linking in ternary
@ 2010-06-25 21:27 Hodapp87 at gmail dot com
  2010-06-25 21:31 ` [Bug c++/44673] " rguenth at gcc dot gnu dot org
                   ` (15 more replies)
  0 siblings, 16 replies; 19+ messages in thread
From: Hodapp87 at gmail dot com @ 2010-06-25 21:27 UTC (permalink / raw)
  To: gcc-bugs

Reproduced on 4.6.0 and 4.1.2 as well.

Here is the header file:
class TestClass
{
public:
        void testFn() const;
private:
        static const int VAR1 = 9;
        static const int VAR2 = 5;
};

And here is the source file including it:
#include "testclass.h"
void TestClass::testFn() const {
        int b = 1;
        int c = (b == 0) ? VAR1 : VAR2;
        int d;
        if (b == 0) d = VAR1;
        else d = VAR2;
}
int main(void) { return 0; }

This compiles, but does not link (g++ is called with no options, only the
source). The following error is given:
gcc-test.o: In function `TestClass::testFn() const':
gcc-test.cpp:(.text+0x14): undefined reference to `TestClass::VAR1'
gcc-test.cpp:(.text+0x1b): undefined reference to `TestClass::VAR2'
collect2: ld returned 1 exit status

If I comment out the line "int c = (b == 0) ? VAR1 : VAR2;" in the source, then
the linker errors disappear.

I have been told that VAR1 and VAR2 are declared and initialized, but not
defined, and therefore this code is not valid C++. If it is indeed invalid,
then why does only the ternary statement cause a linking error, but not the
equivalent if/else?


-- 
           Summary: static const variable works in if/else, fails at linking
                    in ternary
           Product: gcc
           Version: 4.4.1
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: Hodapp87 at gmail dot com
 GCC build triplet: i486-linux-gnu
  GCC host triplet: i486-linux-gnu
GCC target triplet: i486-linux-gnu


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


^ permalink raw reply	[flat|nested] 19+ messages in thread
[parent not found: <bug-44673-4@http.gcc.gnu.org/bugzilla/>]

end of thread, other threads:[~2012-03-02  0:36 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-06-25 21:27 [Bug c++/44673] New: static const variable works in if/else, fails at linking in ternary Hodapp87 at gmail dot com
2010-06-25 21:31 ` [Bug c++/44673] " rguenth at gcc dot gnu dot org
2010-06-25 21:32 ` pinskia at gcc dot gnu dot org
2010-06-25 21:45 ` Hodapp87 at gmail dot com
2010-06-25 21:47 ` pinskia at gcc dot gnu dot org
2010-06-25 23:05 ` redi at gcc dot gnu dot org
2010-06-25 23:08 ` redi at gcc dot gnu dot org
2010-06-26  0:19 ` manu at gcc dot gnu dot org
2010-06-26  0:25 ` pinskia at gcc dot gnu dot org
2010-06-26  0:31 ` manu at gcc dot gnu dot org
2010-06-26  0:31 ` manu at gcc dot gnu dot org
2010-06-26 10:26 ` redi at gcc dot gnu dot org
2010-06-26 10:34 ` redi at gcc dot gnu dot org
2010-06-26 10:35 ` manu at gcc dot gnu dot org
2010-06-26 10:38 ` redi at gcc dot gnu dot org
2010-06-26 10:53 ` manu at gcc dot gnu dot org
2010-06-26 13:46 ` redi at gcc dot gnu dot org
     [not found] <bug-44673-4@http.gcc.gnu.org/bugzilla/>
2010-09-29  9:15 ` pinskia at gcc dot gnu.org
2012-03-02  0:36 ` pinskia 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).