0

I have myself a toolkit containing a bunch of functions. I also have a mainprog which links against this toolkit, so it can use its functions.

Toolkit source:

#include <iostream>

using namespace std;

namespace {
    int foo() {
       cout << "doing foo" << endl;
       return 0;
    }
    int tmp = foo(); // can't just call foo() without an assignment, compiler complains
}

// bunch of useful functions

MainProg source:

#include <iostream>
#include "toolkit_header.h"

using namespace std;

int main() {
       // fiddle about for a while
       cout << "using toolkit functions..." << endl;
       // start using toolkit functions
       return 0;
}

This will output:

doing foo
using toolkit functions...

The ability to have a function from the toolkit execute automatically at the start of each main() which links against it would be very useful for me (there are loads of these, and requiring all mainprogs to explicitly call the function at the start isn't an acceptable solution), but it feels like a Bad Thing. Is there anything drastically wrong with doing this?

I've had a google around but this seems to be a hard question to find, as there are so many hits about calling functions which are in unnamed namespaces.

rbennett485
  • 1,907
  • 15
  • 24
  • How will users see the toolkit? As a library? Or something else. – Anon Mail Dec 08 '15 at 16:47
  • So this is within a larger build system. The toolkit is compiled as a separate unit, and the build system allows mainprogs to specify which toolkits they will link against. tbh I haven't done much cpp outside of this build system, but I imagine that is much how a cpp library would work – rbennett485 Dec 08 '15 at 16:51
  • I asked because you can do some initialization work when a shared library is loaded. See, for example, this link: http://stackoverflow.com/questions/9759880/automatically-executed-functions-when-loading-shared-libraries – Anon Mail Dec 08 '15 at 16:54
  • Yeah, I don't think that's possible in my setup unfortunately – rbennett485 Dec 08 '15 at 16:55
  • See the answers below but I suggest you come up with a global solution. What if others toolkit developers do the same thing and you get a complicated initialization sequence for N toolkits that may become unreliable. – Anon Mail Dec 08 '15 at 17:02

3 Answers3

0

You do realize that your toolkit code is executing BEFORE main is executed? The code as posted is actually 100% OK and not a bad thing in any way.

Problems might arise when you try to use other static objects from other files inside your toolking static initializers, since the order of their initialization is not defined. As a matter of fact, you are already using one static object - std::cout, and unless you are using C++11 or later, cout is not guranteed to be initialized at this time. Starting C++11 you have this guarantee, so this should be OK.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
0

Executing code from within unnamed namespace

Code in an unnamed namespace is executed in exactly the same way as code in named or global namespace.

The ability to have a function from the toolkit execute automatically at the start of each main() ... but it feels like a Bad Thing. Is there anything drastically wrong with doing this?

Actually, the function is executed before main.

There could be a problem, potentially. In case some program depends on the function being called before initialization of a static oject, there is no way to guarantee that happens unless the function is called explicitly, by the code that depends on it.

If it's acceptable that no code during static initialization may depend on the function being executed, then there should be no problem. That said, if you intend the toolkit to be reusable by others, then you probably cannot make that assuption. Users of your toolkit would be thankful if they get to decide when something gets executed - by explicitly calling the function.

You can avoid the kludgy int assignment by using a constructor of a static object:

struct static_init {
    static_init() {
        cout << "doing foo" << endl;
    }
} static_init;
eerorika
  • 232,697
  • 12
  • 197
  • 326
0

Also note, that if foo() or tmp is not used anywhere else in the code (i.e. in the .cpp file as it is unnamed namespace), the linker might optimize it out, so it would not be called at all.

EmDroid
  • 5,918
  • 18
  • 18