Is there a way to ensure the order of static object initialization for certain objects for the entire program. I have memory allocators that I would like to be allocated as the first things in a program, as they will be used else where throughout the program and I want to use these allocators to allocate all later memory.
I understand this is probably compiler specific as I don't believe the C++ standard allows this. The two compilers I am interested in doing this for is gcc and VS2010's compiler. If there is a way, could someone explain how?
EDIT
I do not want "construct on first use" because the allocators will be allocating a large block of memory that I want initialized at the start of the program.
You can somewhat affect the order of initialization by using compiler specific directives. MSVC has a pragma
#pragma init_seg({ compiler | lib | user | "section-name" [, func-name]} )
that can somewhat set the priority for a specific module. See this reference for init_seg.
The gcc compiler has a similar/related attribute syntax for setting the relative priority of a specific initialization. It looks like this
Some_Class A __attribute__ ((init_priority (2000)));
Some_Class B __attribute__ ((init_priority (543)));
and is explained on this page on init_priority.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With