- static initialisation fiasco - 5 Updates
Frederick Gotham <cauldwell.thomas@gmail.com>: Aug 08 02:26AM -0700 Looking at this question in the FAQ: https://isocpp.org/wiki/faq/ctors#static-init-order So let's say you start off with: // File x.cpp #include "Fred.hpp" Fred x; // File y.cpp #include "Barney.hpp" Barney y; // File Barney.cpp #include "Barney.hpp" #include "Fred.hpp" Barney::Barney() { // ... x.goBowling(); // ... } // File main.cpp #include <iostream> #include "Fred.hpp" #include "Barney.hpp" int main(void) { cout << x.Hello() << endl; cout << y.Hello() << endl; } Well the first thing I'd do is change the first two source files like this: // File x.cpp alignas(Fred) char storage_for_x[sizeof(Fred)]; Fred &x = *static_cast<Fred*>(static_cast<void*>(&storage_for_x)); // File y.cpp alignas(Barney) char storage_for_y[sizeof(Barney)]; Barney &y = *static_cast<Barney*>(static_cast<void*>(&storage_for_y)); And then I'd create two function, PreMain and PostMain: void PreMain(void) { ::new(&x) typename std::remove_reference<decltype(x)>::type; ::new(&y) typename std::remove_reference<decltype(y)>::type; } template<class T> void PostMain_Destruct(T *const p) { p->~T(); } void PostMain(void) { PostMain_Destruct(&y); PostMain_Destruct(&x); } And then I'd write 'main' like this: int main(void) { PreMain(); std::atexit(PostMain); cout << x.Hello() << endl; cout << y.Hello() << endl; } |
Bo Persson <bo@bo-persson.se>: Aug 08 01:11PM +0200 On 2020-08-08 at 11:26, Frederick Gotham wrote: > alignas(Barney) char storage_for_y[sizeof(Barney)]; > Barney &y = *static_cast<Barney*>(static_cast<void*>(&storage_for_y)); > [snip] Another option would be to just not have x.goBowling(); in the constructor. What is it doing there in the first place?! This just reminds me of the old "Doctor, doctor, it hurst when I do this!". Bo Persson |
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Aug 08 01:48PM +0100 On 08/08/2020 12:11, Bo Persson wrote: > x.goBowling(); > in the constructor. What is it doing there in the first place?! > This just reminds me of the old "Doctor, doctor, it hurst when I do this!". A correct solution might be: struct Dweeb { // ... }; typedef std::vector<std::shared_ptr<Dweeb>> Dweebs; typedef Dweebs BowlingParty; struct Bowling { Bowling(BowlingParty& Party); }; int main() { Dweebs dweebs{ "Fred", "Barney" }; Bowling(dweebs); } Or to put it another way: PRESENT A REALISTIC FUCKING USE-CASE THAT MAKES ACTUAL SENSE FOR WHATEVER IS ACTUALLY RUBBING YOUR FUCKING RHUBARB. /Flibble -- "Snakes didn't evolve, instead talking snakes with legs changed into snakes." - Rick C. Hodgin "You won't burn in hell. But be nice anyway." – Ricky Gervais "I see Atheists are fighting and killing each other again, over who doesn't believe in any God the most. Oh, no..wait.. that never happens." – Ricky Gervais "Suppose it's all true, and you walk up to the pearly gates, and are confronted by God," Byrne asked on his show The Meaning of Life. "What will Stephen Fry say to him, her, or it?" "I'd say, bone cancer in children? What's that about?" Fry replied. "How dare you? How dare you create a world to which there is such misery that is not our fault. It's not right, it's utterly, utterly evil." "Why should I respect a capricious, mean-minded, stupid God who creates a world that is so full of injustice and pain. That's what I would say." |
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Aug 08 01:53PM +0100 On 08/08/2020 13:48, Mr Flibble wrote: > Bowling(dweebs); > } > Or to put it another way: PRESENT A REALISTIC FUCKING USE-CASE THAT MAKES ACTUAL SENSE FOR WHATEVER IS ACTUALLY RUBBING YOUR FUCKING RHUBARB. Of course my correct solution has glaring errors but Usenet posts are read only and I don't actually give a fuck. I leave finding the glaring errors as an exercise for the reader. /Flibble -- "Snakes didn't evolve, instead talking snakes with legs changed into snakes." - Rick C. Hodgin "You won't burn in hell. But be nice anyway." – Ricky Gervais "I see Atheists are fighting and killing each other again, over who doesn't believe in any God the most. Oh, no..wait.. that never happens." – Ricky Gervais "Suppose it's all true, and you walk up to the pearly gates, and are confronted by God," Byrne asked on his show The Meaning of Life. "What will Stephen Fry say to him, her, or it?" "I'd say, bone cancer in children? What's that about?" Fry replied. "How dare you? How dare you create a world to which there is such misery that is not our fault. It's not right, it's utterly, utterly evil." "Why should I respect a capricious, mean-minded, stupid God who creates a world that is so full of injustice and pain. That's what I would say." |
Sam <sam@email-scan.com>: Aug 08 01:42PM -0400 Frederick Gotham writes: > // File y.cpp > alignas(Barney) char storage_for_y[sizeof(Barney)]; > Barney &y = *static_cast<Barney*>(static_cast<void*>(&storage_for_y)); There are far simpler ways to solve the static initialization order fiasco that does not require jumping through your own asshole like that, or any similar ugliness. Fred &getFred() { static Fred real_fred; return real_fred; } Calling getFred() from any translation unit makes the right thing will happen. Ditto for Barney. |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment