jiggunjer
jiggunjer

Reputation: 2116

Explicitly declare the global namespace

Is it possible to declare a variable or function in the global namespace from within another namespace? Currently my code looks like this.

namespace test {
//do stuff
}

//declare global stuff

namespace test{
//continue stuff
}

The reason this question came up was because I am using an #include inside a namespace, but part of the included code needs to be in the global namespace. So I can not interrupt or code outside my namespace block.

So I would like:

namespace {// do stuff
//declare global stuff
//do more stuff
}

Now my code looks a bit like:

namespace test{
void majorfcn1();
Void majorfcn2();
#include "minorfunctions.h"
}

Which would be no problem if it just had functions. But in my case I threw some other includes in there that of course should be in the global space. So instead of cutting them out I was hoping I could keep them there and just surround them by a namespace global{} or something.

Upvotes: 5

Views: 1840

Answers (1)

Phil
Phil

Reputation: 6164

The answer is no. I suggest you not #include from inside a namespace. That is not conventional C++ coding practices.

You said you would like:

namespace {// do stuff
//declare global stuff
//do more stuff
}

Then you can just do:

namespace {// do stuff
}

//declare global stuff

namespace {// do stuff
//do more stuff
}

Upvotes: 2

Related Questions