{"action":"created","issue":{"url":"https://api.github.com/repos/eschkufz/x64asm/issues/168","labels_url":"https://api.github.com/repos/eschkufz/x64asm/issues/168/labels{/name}","comments_url":"https://api.github.com/repos/eschkufz/x64asm/issues/168/comments","events_url":"https://api.github.com/repos/eschkufz/x64asm/issues/168/events","html_url":"https://github.com/eschkufz/x64asm/issues/168","id":58961004,"number":168,"title":"static map members of Label have undefined interaction with static data structures","user":{"login":"eschkufz","id":637102,"avatar_url":"https://avatars.githubusercontent.com/u/637102?v=3","gravatar_id":"","url":"https://api.github.com/users/eschkufz","html_url":"https://github.com/eschkufz","followers_url":"https://api.github.com/users/eschkufz/followers","following_url":"https://api.github.com/users/eschkufz/following{/other_user}","gists_url":"https://api.github.com/users/eschkufz/gists{/gist_id}","starred_url":"https://api.github.com/users/eschkufz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eschkufz/subscriptions","organizations_url":"https://api.github.com/users/eschkufz/orgs","repos_url":"https://api.github.com/users/eschkufz/repos","events_url":"https://api.github.com/users/eschkufz/events{/privacy}","received_events_url":"https://api.github.com/users/eschkufz/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/eschkufz/x64asm/labels/bug","name":"bug","color":"fc2929"},{"url":"https://api.github.com/repos/eschkufz/x64asm/labels/low.","name":"low.","color":"009800"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-02-25T20:04:24Z","updated_at":"2015-02-27T07:22:57Z","closed_at":"2015-02-27T07:10:07Z","body":"C doesn't guarantee an initialization order on static data structures. This causes Label's static maps to behave strangely when used as part of other static initialization code.\r\n\r\nSpecifically, see trying to create Code in a default constructor that is used as part of a cpputil arg."},"comment":{"url":"https://api.github.com/repos/eschkufz/x64asm/issues/comments/76349854","html_url":"https://github.com/eschkufz/x64asm/issues/168#issuecomment-76349854","issue_url":"https://api.github.com/repos/eschkufz/x64asm/issues/168","id":76349854,"user":{"login":"eschkufz","id":637102,"avatar_url":"https://avatars.githubusercontent.com/u/637102?v=3","gravatar_id":"","url":"https://api.github.com/users/eschkufz","html_url":"https://github.com/eschkufz","followers_url":"https://api.github.com/users/eschkufz/followers","following_url":"https://api.github.com/users/eschkufz/following{/other_user}","gists_url":"https://api.github.com/users/eschkufz/gists{/gist_id}","starred_url":"https://api.github.com/users/eschkufz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eschkufz/subscriptions","organizations_url":"https://api.github.com/users/eschkufz/orgs","repos_url":"https://api.github.com/users/eschkufz/repos","events_url":"https://api.github.com/users/eschkufz/events{/privacy}","received_events_url":"https://api.github.com/users/eschkufz/received_events","type":"User","site_admin":false},"created_at":"2015-02-27T07:22:57Z","updated_at":"2015-02-27T07:22:57Z","body":"This is the singleton design pattern.\n\nYou can take a look at cpputil::Singleton<T> for an example. It's basically\na templated version of this concept.\n\nOn Thu, Feb 26, 2015 at 11:21 PM, eric schkufza <eric.schkufza@gmail.com>\nwrote:\n\n> C doesn't guarantee an initialization order among static variables in\n> different translation units. So this code is undefined --\n>\n> // a.h\n> class X {\n>  static std::map<int,int> m; // m is defined\n> }\n>\n> // a.cc\n> std::map<int,int> X::m // m is declared\n>\n> // b.cc\n> #include \"a.h\"\n> int global = X::m[0] // m is dereferenced\n>\n> You can't guarantee that m has been declared / initialized when static\n> storage and initial values for global are computed.\n>\n> The reason this was a bug was because the cpputils arg library does a ton\n> of stuff with static variables and it was having a strange interaction with\n> the static maps inside the label class.\n>\n> The solution is something like this --\n>\n> // a.h\n> class X {\n>   static std::map<int,int>& m() {\n>     static std::map<int,int> m; // m is defined and declared\n>     return m;\n>   }\n> }\n>\n> // a.cc\n> // nothing here now\n>\n> // b.cc\n> #include \"a.h\"\n> int global = X::m()[0] // m is dereferenced\n>\n> The semantics are identical, but the initialization code that the compiler\n> emits is forced into a function which imposes an ordering on the evaluation\n> of the dereference in the assignment to global. The map has to be\n> initialized before the index takes place.\n>\n> On Thu, Feb 26, 2015 at 11:13 PM, Berkeley Churchill <\n> notifications@github.com> wrote:\n>\n>> At some point could you explain why the behavior was undefined to me?\n>> This seems like one of those C++ things I'm not familiar with.\n>>\n>> On 02/26/2015 11:10 PM, eric schkufza wrote:\n>> >\n>> > Closed #168 <https://github.com/eschkufz/x64asm/issues/168> via\n>> > e287b5f\n>> > <\n>> https://github.com/eschkufz/x64asm/commit/e287b5fffc1886c2219f2c7aba97e779cf82d91e\n>> >.\n>> >\n>> > —\n>> > Reply to this email directly or view it on GitHub\n>> > <https://github.com/eschkufz/x64asm/issues/168#event-243092766>.\n>> >\n>>\n>> —\n>> Reply to this email directly or view it on GitHub\n>> <https://github.com/eschkufz/x64asm/issues/168#issuecomment-76349159>.\n>>\n>\n>\n"}}