Re: [boost] Is there any interest in static constructors?

Hello Mathias, I'm afraid you didn't get the point. Let me explain.
The object will only be constructed when instance() is called
Technically, yes, the instance will be create once singleton::instance is called for the first time, but in order to make it constructed BEFORE main, singleton::instance has to be called during static data initialization, and static_init GUARANTIES that this static initialization will be performed before main WITH NO additional calls.
If the constructor of the object depends on other singletons, then instance() of those other singletons will naturally be called before, and therefore they will end up being constructed before.
You're right, if you refer to singleton A's before creating singleton B's instance, to order of initialization would be correct. But let's no limit this case to singletons only, where initialization is achieved in just one call. There can many many different situation when you have to perform some additional data initialization and manipulation before creating singleton instance. Where would you put this code? Would you make singleton constructor or instance access method dependent on all this data when it naturally should not be? Anyway it doesn't solve the problem for the first call to a singleton if it has to be initialized before main. Unless you create a global static variable and make the first call to singleton during this variable initialization, but then you still need to refer to this variable from the code somewhere in order to make sure your compiler does not eliminate it due to optimization reasons, and this way you will introduce unnecessary dependency. 3. There is no construction recursion. If you did notice it somewhere, please point to exact place. 4. Static data construction order is undefined by C++ standard fixing it requires either changing the standard or getting rid of class and global static data. So, again, to make the idea crystal clear, the initial goal was: 1. Guarantee that static constructor method is called before main is executed 2. No additional calls in either global or any local scope should precede static constructor invocation in order not to introduce extra dependencies and eliminating a situation when initialization invocation code does not get called for any reason. 3. Provide a way to control the order of static constructors calls. No recursions are allowed. The described approach fully satisfies the three simple requirements. If anybody sees possible errors or ways to improve it, feedbacks are very appreciated. Thank you. ______________________________ With best regards, Alexander Stoyan

On 1 February 2013 17:09, Alexander Stoyan <alexander.stoyan@gmail.com> wrote:
Hello Mathias,
I'm afraid you didn't get the point. Let me explain.
Without walking through the implementation in details, I didn't get it at first sight either.
The object will only be constructed when instance() is called
Technically, yes, the instance will be create once singleton::instance is called for the first time, but in order to make it constructed BEFORE main, singleton::instance has to be called during static data initialization, and static_init GUARANTIES that this static initialization will be performed before main WITH NO additional calls.
IMHO, the problem is, that you don't explain this important point in your blog (which I assume serves documentation purpose). The three crucial principles of the idea seem to be as follows: 1. singleton::instance is called during static initialisation. 2. static_unit guarantees that the requirement 1. is fulfilled. 3. static_unit realises the guarantee by... But, they don't stand out from your blog clearly. Best regards, -- Mateusz Loskot, http://mateusz.loskot.net

On 01/02/2013 18:09, Alexander Stoyan wrote:
There can many many different situation when you have to perform some additional data initialization and manipulation before creating singleton instance. Where would you put this code? Would you make singleton constructor or instance access method dependent on all this data when it naturally should not be?
What's the functional difference between putting it as a type list and putting it as code?
Anyway it doesn't solve the problem for the first call to a singleton if it has to be initialized before main.
If you want it to be initialized before main, then use global variables or static members. But then you have to make sure no threads exist prior to entering main. Unlike for static variables in functions, global constructors are not thread-safe.
but then you still need to refer to this variable from the code somewhere in order to make sure your compiler does not eliminate it due to optimization reasons, and this way you will introduce unnecessary dependency.
I have no idea what you're talking about. The compiler cannot remove things if it changes observable behaviour, outside of a few situations where copy constructors can be elided.
3. There is no construction recursion. If you did notice it somewhere, please point to exact place.
There is an "isinside" boolean.
4. Static data construction order is undefined by C++ standard fixing it requires either changing the standard or getting rid of class and global static data.
A given order can still be forced.
participants (3)
-
Alexander Stoyan
-
Mateusz Loskot
-
Mathias Gaunard