3 ms·
The approach I laid out makes sure that the constructors for all of your global/static objects get called. AFAIK, there's no reason you couldn't do everything "
by mrkline 10y ago
The approach I laid out makes sure that the constructors for all of your global/static objects get called. AFAIK, there's no reason you couldn't do everything "by hand", but it seems like a lot more work for questionable gain.
- shortoncash 10y agoValid point. However, there's no reason it has to be overly manual. As an example, you can make all the global object classes mixins for a variadic and use a variadic constructor and member methods to bring them all online in one fell swoop, even parameterizing construction via policy classes as template parameters. (Plus you have the added benefit of knowing via sizeof() on the variadic type the footprint of all the global objects.) The gain would be to avoid the dependence on the linker script solution, particularly if tweaking linker scripts on a different compiler/build environments.
- Espressosaurus 10y agoI prefer forcing a top-down initialization using placement new for any extent globals, that way initialization order can be carefully controlled. E.g. the OS gets started before we even bother bringing anything else up, other initialization happens in a separate task, then worker tasks are brought up, etc. Everything is sequenced, and we don't have to go to the linker script to figure out what's actually getting called. Since the globals are singletons, any attempt to get their instance when they've already initialized is an error that will blow the program up. Helps keep us honest.