Question

I have a C++ project (VC++ 2008) that only uses the std namespace in many of the source files, but I can't find the "right" place to put "using namespace std;".

If I put it in main.cpp, it doesn't seem to spread to my other source files. I had it working when I put this in a header file, but I've since been told that's bad. If I put it in all of my .cpp files, the compiler doesn't recognize the std namespace.

How should this be done?

Was it helpful?

Solution

You generally have three accepted options:

  1. Scope usage (std::Something)
  2. Put using at the top of a source file
  3. Put using in a common header file

I think the most commonly accepted best practice is to use #1 - show exactly where the method is coming from.

In some instances a file is so completely dependent on pulling stuff in from a namespace that it's more readable to put a using namespace at the top of the source file. While it's easy to do this due to being lazy, try not to succumb to this temptation. At least by having it inside the specific source files it's visible to someone maintaining the code.

The third instance is generally poor practice. It can lead to problems if you're dependent on more than one external source file both of which may define the same method. Also, for someone maintaining your code it obfuscates where certain declarations are coming from. This should be avoided.

Summary: Prefer to use scoped instances (std::Something) unless the excessive use of these decreases the legibility and maintainability of your code.

OTHER TIPS

In your headers, the best thing I think is just to fully qualify the namespace, say of a member

#include <list>

class CYourClass
{
    std::list<int> myListOfInts;
    ...
};

You can continue to fully qualify in any function in a cpp

int CYourClass::foo()
{
    std::list<int>::iterator iter = myListOfInts.begin();
    ...
}

you really never need "using namespace std" anywhere. Only if you find you are typing std:: too much, thats when its good to throw in a "using namespace std" to save your own keystrokes and improve the readability of your code. This will be limited to the scope of the statement.

int CYourClass::foo()
{
    using namespace std;
    list<int>::iterator iter = myListOfInts.begin();
    ...
}

You can put it in multiple places - in each .cpp file.

Essentially, you need to make a choice:

1) "do the right thing" by including using namespace std in all your cpp files, or

2) add using namespace std in some common header file.

Different people will have different opinions of which is best, however if you ask me, I'd chose to put using namespace std in a common header. In my opinion, the std namespace is just that - "standard" and any name conflicts need to be resolved at code level, rather than relying on namespaces.

The namespace doesn't 'spread' to other files. You have to put it in each of the files, or just explicitly call out your classes.

either:

using namespace std; blah << x;

or:

std::blah << x;

The choice is a style choice, either works in practice.

If the compiler doesn't 'recognize' the namespace it's because you haven't included the definition file that declares it ( i.e. include )

It's not a good idea to spread over your code using namespace std;only because it's seem convenient to you to do so. But if you insist on it you can wrap your own code into that name space. And only later when you will actually will make a use of your function/classes in the main file you will define using namespace std;. Just to emphasize that I'm saying, here the example:

namespace std
{
   class MyNewClass
   {
       public:
             MyNewClass( ) { out << "Hello there" << endl;};
   };
}; 

int main( )
{
    std::MyNewClass tmp;
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top