I'm working on a small personal C++ project using STL. I don't like having "std::
" all over the place in my header files, as I find it hinders readability, but at the same time I don't want to cause myself problems later on by putting using namespace std
in a header file.
So I'm wondering if there's a way to limit the scope of the using
declaration, so that it applies to the contents of my header file, but doesn't apply to files that include it. I tried various things like this
{
using namespace std;
// header file contents here
}
but it seems that introducing a scope this way is not allowed outside of a function definition. Is there a way to do what I'm after?
Please note: I'm really not interested in discussing whether this is a good idea, I just want to know if it can be done.
You should definitely NOT use using namespace in headers for precisely the reason you say, that it can unexpectedly change the meaning of code in any other files that include that header. There's no way to undo a using namespace which is another reason it's so dangerous.
Code in header files should always use the fully qualified namespace name. The following example shows a namespace declaration and three ways that code outside the namespace can accesses their members.
Since you can't put a namespace using statement at the top level of the header file, you must use a fully qualified name for Standard Library classes or objects in the header file. Thus, expect to see and write lots of std::string, std::cout, std::ostream, etc. in header files.
While this practice is okay for example code, pulling in the entire std namespace into the global namespace is not good as it defeats the purpose of namespaces and can lead to name collisions. This situation is called namespace pollution.
Yes, I think that can be done.
In order to achieve that you need to build your own namespace. I have written some code which is working as per the expected.
Header file looks like:
#include <iostream>
namespace my_space {
using namespace std;
void mprint ()
{
/*
* This is working. It means we can access
* std namespace inside my_space.
*/
cout << "AAA" << endl;
}
};
Implementation file looks like:
#include "my_header.h"
int main ()
{
/*
* Working Fine.
*/
my_space::mprint();
/*
* It gives a compile time error because
* it can't access the std namespace
*/
cout << "CHECK" << endl;
return 0;
}
Please let me know if this does not satisfy your requirement. We can work out on that.
No, it cannot be done. Your attempt to introduce a scope in the header failed exactly because there is no such thing as a header scope - header files do not have a special status during compilation. The translation units are source file obtained after the preprocessor is done with it. Thus, all the include
directives simply expand the corresponding header files. This prevents you from making the contents of the header file context-specific in any way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With