This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Constant singleton object initialization
- From: Ian Lance Taylor <iant at google dot com>
- To: Helge Bahmann <hcb at chaoticmind dot net>
- Cc: gcc-help at gcc dot gnu dot org
- Date: 10 Jul 2007 07:22:31 -0700
- Subject: Re: Constant singleton object initialization
- References: <200707101111.25720.hcb@chaoticmind.net>
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