Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explicitly declare the global namespace

Tags:

c++

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.

like image 929
jiggunjer Avatar asked Oct 30 '22 22:10

jiggunjer


1 Answers

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
}
like image 169
Phil Avatar answered Nov 15 '22 04:11

Phil