Hi,
In a function, i need to make sure i am after main() has started in order to create a specific object used by a library (because it needs to make sure some other data has been created). Would you know about any tricks to find if the current running code is in main() and not before?
I'm guessing it won't be portable, so i'm looking for a solution on windows and one for linux if possible
Gaminic, constructors of global data for example are run before main().
Moschops, my code is supposed to be easily usable in any project, so i would prefer a trick that only needs code in my function, rather than something that requires me to mofify each project's main()
You can't, global variables are always constructed before main. Why would you need that anyway? Can't you just initialize some variables before the others?
bool& HasMainStarted()
{
staticbool started = false;
return started;
}
int main()
{
HasMainStarted() = true;
// ...
}
void AnyWhereElse()
{
if(!HasMainStarted())
{
// main has not started yet
}
}
The key to this trick is that local static vars are constructed on first use. So 'started' here will always be properly constructed when you access it.
But this is sort of a hacky work around. The better solution here would be to just not have global objects and avoid this mess entirely.
There is nothing wrong with calling an initialization function like Disch and L B suggested, OpenGL does this and it's popularity has not suffered for it. This sounds like more of a design issue but we would need to have more context to know how to help.
I've not seen this before, and I'll put my hand up as not quite following how it works and to ask Disch for amplifying remarks. I'm au fait with the local statics being constructed on first use; what's the effect of the = true on the end of HasMainStarted() = true;?
HasMainStarted() returns a reference to the static boolean in the function's scope. By assigning to this reference you change the value of that boolean without asking the function to do it itself.
Okey dokey. That's where my experimental code was going, but it didn't seem to change the value... I expect I just fumbled the code.. although the debugger should have seen it. GARGH! Expect I fumbled that too.
Did you make sure that function returned "bool &" and not "bool"? In the second case it only assigns to a temporary, which is allowed but unhelpful in this case.
In some cases, it is beutiful; Consider an INI class that reads from a string containing an INI file and then allows you to get and set values through operator() by returning a string reference. It looks syntaxtically nice:
What? Return values are rvalues. If the return type was bool instead of bool &, it wouldn't even compile.
Actually, there's nothing wrong in returning a reference to a static variable created inside a function. This static variable will exist throughout the program (after it's first accessed), but will only be visible inside that function (unless you return a reference or pointer, of course, as we are doing).