Bug 20484 - No proper linkage created for in class initialized static const
Summary: No proper linkage created for in class initialized static const
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.3.5
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-03-15 15:45 UTC by Bart Dopheide
Modified: 2005-07-23 22:49 UTC (History)
1 user (show)

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


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Bart Dopheide 2005-03-15 15:46:01 UTC
Example:
template <typename T>
T const max1(T const a, T const b)
{ return a < b ? b : a; }

template <typename T>
T const & max2(T const & a, T const & b)
{ return a < b ? b : a; }

struct Test
{ static int const A = 1; };

int main()
{
#if 0
  max1(Test::A, 2);     // OK.
#else
  max2(Test::A, 2);     // Does not link: undefined reference to `Test::A
#endif
}

The problem does not appear in 2.95.4
The problem does appear in 3.0.4, 3.2.3
Also appeared on 3.4.4 x86_64
Comment 1 Andrew Pinski 2005-03-15 16:12:30 UTC
You need to declare space for the variable still like so:
int const Test::A;

This is invalid.
Comment 2 Bart Dopheide 2005-03-15 18:46:56 UTC
You are correct. I was wrong.

I thought, because of consistency reasons, that
1) both should link, or
2) both shouldn't link.
(And I choose 1) for the "bug"-report.)

As I now understand it, the "static int const A = 1;" will be seen as an
"integral constant expression". If, OTOH, a reference has to be taken, it is
seen as a "normal int", thus requiring an actual definition. (It is in 9.4.2 of
the standard.)

See also bugs 14404, 13259, 15244, 17673.

(I search the bugs database before filing, but could not find it.)

It stills /looks/ inconsistent though.
Comment 3 Andrew Pinski 2005-03-15 18:50:37 UTC
Subject: Re:  No proper linkage created for in class initialized static const


On Mar 15, 2005, at 1:46 PM, dopheide at fmf dot nl wrote:

> It stills /looks/ inconsistent though.

That is because there is an optimization going on here which is allowed
by the standard.


-- Pinski