Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limiting the scope of "using namespace" in a header file

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.

like image 931
N. Virgo Avatar asked Jul 04 '14 02:07

N. Virgo


People also ask

Why should we avoid use of using namespace in header files?

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.

Should namespaces be in header files?

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.

Can you use namespace std in a header file?

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.

Why is using namespace a bad practice?

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.


2 Answers

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.

like image 140
Abhishek Mittal Avatar answered Sep 21 '22 00:09

Abhishek Mittal


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.

like image 39
Pradhan Avatar answered Sep 23 '22 00:09

Pradhan