Static initializers

I'm fiddling around with a possible way to run some chunks of "this stuff needs to be initialized at program start" code while keeping them local to their respective modules, and came up with this:

static struct init {
    init() {
        // do stuff
    }
} _;

When I put that in module a.cc it worked fine. When I put it also in module b.cc not so good - A's version was called twice and B's version not at all. I was figuring okay, compiler bug, it's getting confused about the two functions with the same name, but what surprised me was that on further testing, it behaves exactly the same way in Microsoft C++ and GCC. Do both compilers happen to have the same bug, or is there something I'm missing about the language semantics?

Also, any recommendations for workarounds or other ways to achieve the same result (apart from exporting the relevant functions and explicitly calling them from main which is obviously the fallback)?

Answers


The variables are static and therefore visible only in their respective translation units, but the types aren't. How about putting your structs in anonymous namespaces?


Need Your Help

Specific day of current month and year

sql sql-server tsql datetime

I have problem with return of specific day of current month and year. I need for example 15th day. Until now I used in FB/IB existing function:

UIImagePicker reappears after being image is selected

ios objective-c uiimage uiimagepickercontroller

I have a UIImagePicker that keeps reappearing after I have selected an image. My code is checking for a camera and then displaying the UIImagePicker based on the fact that there is no camera in the