tags:

views:

97

answers:

4

I declared a global function in a .cpp file void functionA(). I would like functionA() to be called exactly once before the start-up ( not inside main()). The thing I realize is if the function is int functionB(), I could call it using static int A = functionB(). But for return value of void, how could I do that?

Thanks

+5  A: 

You put it into the constructor of a global object:

void functionA();

namespace {
  struct global_initializer {
    global_initializer() {functionA();}
  } the_global_initializer;
}

Note that this has the common drawbacks of global initialization: While globals within the same translation unit are initialized in the order of their definition, the order of initialization of globals across translation units is undefined.

Also, linkers might choose to eliminate unreferenced objects (the_global_initializer), which would prevent functionA() from being called.

sbi
I'm thinking it's right but I have a doubt : isn't it compiler implementation dependent?
Klaim
@Klaim: Why would it depend on the compiler? The `global_initializer` instance has to be created, unconditionally.
Chris Jester-Young
+1, but there's a subtle caveat. If that global object is in a static library it might be not linked into the final executable and so the function will not be called.
sharptooth
@sharptooth: That's why I added the last sentence. Do you feel it doesn't correctly state that and I should improve on it?
sbi
@sbi: Well, it is clear for you and it is clear for me since we know in details why and when it happens. Maybe newbies need an explicit note that in this case construction code is not invoked and that this is a typical yet subtle problem.
sharptooth
@sharptooth: You're right, I expanded that part a bit.
sbi
A: 

You could use a static struct, it's constructor will get called before main.

#include <iostream>

void functionA(void)
{
    std::cout << "Hello, ";
}

static struct BeforeMain
{
    BeforeMain(void)
    {
        // stuff in this constructor is executed before "main" function
        functionA();
    }
} g_beforeMain; // shouldn't get used though

int main(void)
{
    std::cout << "world!" << std::endl;
    return 0;
}

This will print Hello, world!, although I'm new to C++, so this may not be the best approach.

dreamlax
A: 

Solution 1: Make that void function to have a return type (say int) and return a dummy return value

If you can't do Solution 1,

Solution 2: Write a wrapper function as shown. Make sure to write code so that wrapper can be called only once (unless it is fine to do so multiple times)

Wrap it up! (and document extensively).

void fA(){}

int wrapper(){
   // Have checks here to ensure it is not called more than once
   fA(); 
   return 0;
}

// Extensively document such as `'x' is a bogus dummy variable.`
static int x = wrapper();

int main(){
}
Chubsdad
+3  A: 

static int a = functionA(), 42;

There are few places where comma expressions are useful, but this may be one of them.

MSalters
+1: This is absoultely creative!
Chubsdad