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.