Reputation: 33365
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)?
Upvotes: 0
Views: 105
Reputation: 19971
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?
Upvotes: 2