Tricks to know if main() has started?

Pages: 12
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
If you just need to see it, stick std::cout << "Entered main" << std::endl; as the first line of the main function.

If the function needs it, have a global value enteredMain that is set to zero at start, and set to one by main. Your function can check the value.
Last edited on
I mean, i need a way for the function to know if it is before main start, not for the user.
How exactly can any code run before your main? Isn't that your point of entry?
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?
Yes, something like this:
1
2
int MyGlobalData = InitMyGlobalData(); //can do anything it needs to
#include "TheLibraryICannotChange.h" 
The constructor for the "specific object" could take a const reference to the "other data"

That way the user of your library is forced to create the "other data" before they can create the "specific object"

Its a funny sort of dependency where the "specific object" doesn't require any access to the "other data" even though it depends on it?
Quick and dirty way to do it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool& HasMainStarted()
{
  static bool 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.
Yes, I really can't see where it could be used. Perhaps if you explain your situation we could provide more specific help.
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;?
Last edited on
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.
Last edited on
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.
I can't work out if this is beautiful or hideous :) Which reminds me of my ex, and that didn't work out well either :p
Last edited on
Use the "construct on first use" idiom.

There are a variety of ways to do it. I favor a proxy object that defers to a non-proxy object when it is first used.

Good luck!
What? Return values are rvalues. If the return type was bool instead of bool &, it wouldn't even compile.
I can't work out if this is beautiful or hideous

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:
1
2
3
4
if(MyIni("Some Group", "Some Item") == "lol")
{
    MyIni("Some Group", "Some Other Item") = "trolol";
}


We're getting a bit off topic...
Last edited on
helios wrote:
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).

Edit
Oh, sorry, I misunderstood your post.
Last edited on
Pages: 12