Bug 42101 - Linking failure with static constants and ternary inline function
Summary: Linking failure with static constants and ternary inline function
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.4.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
: 42241 42330 43594 44215 44528 (view as bug list)
Depends on:
Blocks:
 
Reported: 2009-11-19 01:42 UTC by David Anderson
Modified: 2010-06-13 17:53 UTC (History)
9 users (show)

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


Attachments
reduced test case (332 bytes, text/plain)
2009-11-19 01:44 UTC, David Anderson
Details

Note You need to log in before you can comment on or make changes to this bug.
Description David Anderson 2009-11-19 01:42:21 UTC
When using private, static constants in a class declaration, and using these constants in an inline function that returns a ternary expression , gcc produces linker errors.

This seems to be a problem going back to at least 4.0, and I can reproduce it against 4.4.1. I have not tried anything earlier than 4.0 or later than 4.4.1.
Comment 1 David Anderson 2009-11-19 01:44:19 UTC
Created attachment 19039 [details]
reduced test case

This program doesn't link, though it should.

keima:src dvander$ g++ test.cpp -o test
test.cpp: In function ‘int main()’:
test.cpp:28: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘size_t’
Undefined symbols:
  "X::LENGTH_MASK", referenced from:
      X::dependentLength() const in ccdhJ0JB.o
  "X::DEPENDENT_LENGTH_MASK", referenced from:
      X::dependentLength() const in ccdhJ0JB.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Comment 2 Andrew Pinski 2009-11-19 01:46:51 UTC
You don't have a definition of the static const variable which you need for this to be valid C++.
Comment 3 Luke Wagner 2009-11-19 01:52:22 UTC
(In reply to comment #2)
> You don't have a definition of the static const variable which you need for
> this to be valid C++.
Comment 4 Luke Wagner 2009-11-19 01:53:40 UTC
(In reply to comment #3)
> (In reply to comment #2)
> > You don't have a definition of the static const variable which you need for
> > this to be valid C++.

Heh, oops, hit 'Commit' accidentally.

This example does have a definition for the static member variable.
Comment 5 David Anderson 2009-11-19 01:58:54 UTC
I'll also note that the compiler seems to accept it, as I get a .o file. The linking step botches.
Comment 6 Paolo Carlini 2009-11-19 02:16:30 UTC
It doesn't have any definition, it does have a *declaration*. This is pretty basic C++, by the way. Just add out of class:

  const size_t X::DEPENDENT_LENGTH_MASK;
  ...
  ...

and everything will be fine. The compiler doesn't diagnose that, right, because the actual diagnosis, as for any undefined entity, eg, functions, happens only at link time, the linker tells you: as happens for functions, you could well have the definitions in another file.
Comment 7 Luke Wagner 2009-11-19 02:41:37 UTC
(In reply to comment #6)

> This is pretty basic C++, by the way.

Mmm hmm. SO basic that it warranted special clarification in 9.4.2-4:

"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."
Comment 8 Paolo Carlini 2009-11-19 09:38:42 UTC
It is basic, yes, a point worth making with people insisting that we do have a serious bug, thus reopening the PR at will, without trusting the competence of the maintainers and wasting some of our time.
Comment 9 Andrew Pinski 2009-12-01 17:07:43 UTC
*** Bug 42241 has been marked as a duplicate of this bug. ***
Comment 10 Paolo Carlini 2009-12-08 09:18:30 UTC
*** Bug 42330 has been marked as a duplicate of this bug. ***
Comment 11 Jonathan Wakely 2009-12-08 09:48:54 UTC
*** Bug 42330 has been marked as a duplicate of this bug. ***
Comment 12 Andrew Pinski 2010-03-31 00:14:17 UTC
*** Bug 43594 has been marked as a duplicate of this bug. ***
Comment 13 Andrew Pinski 2010-05-20 17:53:57 UTC
*** Bug 44215 has been marked as a duplicate of this bug. ***
Comment 14 Paolo Carlini 2010-06-13 17:53:19 UTC
*** Bug 44528 has been marked as a duplicate of this bug. ***