This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Constant singleton object initialization
- From: Helge Bahmann <hcb at chaoticmind dot net>
- To: gcc-help at gcc dot gnu dot org
- Date: Tue, 10 Jul 2007 11:11:25 +0200
- Subject: Constant singleton object initialization
Hello,
Assume the following very similiar class declarations:
class A {
public:
int x;
};
class B {
public:
inline B(int _x) : x(_x) {}
int x;
};
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?)
I have a program which declares a large amount of singleton objects in this
way, I would like them to have virtual methods, and all constructors are
empty. The problem is that the initialization of the objects is really eating
into the start-up overhead. I could replace the classes with virtual methods
with struct containing function-pointer tables, but this appears very
un-C++-ish...
Thanks and best regards
--
Mathematicians stand on each other's shoulders while computer scientists stand
on each other's toes.
-- Richard Hamming