Bug 24312 - C++ front-end doesn't correctly handle distinction between global initializers and ctors
Summary: C++ front-end doesn't correctly handle distinction between global initializer...
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: unknown
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2005-10-11 17:14 UTC by Chris Lattner
Modified: 2007-10-04 01:45 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 2.95.3 3.0.4 3.2.2 3.2.3 3.3.1 4.0.0 3.4.0 4.1.0
Last reconfirmed: 2006-01-26 05:32:41


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Chris Lattner 2005-10-11 17:14:06 UTC
The following testcase is miscompiled:

#include <stdio.h>
double foo() { return 1.0; }
static struct S { S(); } s;
static const double Y = foo()+2.0;

S::S() {
  printf("%f\n", Y);
}
int main() {
  printf("%f\n", Y);
}

Both printfs should print 3 based on Mark's insight in Comment #22 of bug 21089.

-Chris
Comment 1 Chris Lattner 2005-10-11 17:15:54 UTC
Note that this could be handled in a straight-forward way with init priorities.
Comment 2 Andrew Pinski 2005-10-11 17:18:20 UTC
Confirmed, not a regression.
Comment 3 Andrew Pinski 2005-10-27 16:22:22 UTC
Hmm, ICC gets it wrong too, maybe there is a misunderstanding somewhere.
Comment 4 Jason Merrill 2007-10-04 01:45:10 UTC
This is not a bug; Mark's comment in bug 21089 had to do with the fact that [basic.start.init] says that static initialization happens before dynamic initialization.  Using foo() in Y's initializer makes Y dynamically initialized, so it is not required to be initialized before s.