Constant singleton object initialization

Ian Lance Taylor iant@google.com
Tue Jul 10 14:23:00 GMT 2007


Helge Bahmann <hcb@chaoticmind.net> writes:

> class C {
> public:
> 	inline C(int _x) : x(_x) {}
> 	virtual ~C(void) {}
> 	int x;
> };
> 
> Now assume the following definitions:
> 
> const A a={42};
> const B b(42);
> const C c(42);
> 
> If I compile this, a will end up in the ".rodata" section with the correct 
> initialization value written to the segment, while b and c end up in BSS and 
> are initialized at runtime. While I think that at least b could reasonably be 
> put into rodata, I am more interested in c -- is there a fundamental reason 
> why this is not possible? (i.e. is this an artifact of the presence of a 
> constructor, or does the virtual methods table pointer affect initialization 
> as well?)

There is no fundamental reason why it is not possible.

Unfortunately, as you've seen, gcc does not implement this
optimization.

This is listed in the bug database:

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

which as you can see does not mean that it is going to be fixed any
time soon.

Ian



More information about the Gcc-help mailing list