views:

648

answers:

6

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?

+2  A: 

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();
    ...
}
Doug T.
A: 

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

therefromhere
Like I said, I tried this and I'm getting this compiler error:error C2871: 'std' : a namespace with this name does not existIt works for main.cpp but not for any other of my cpp files.
Tony R
Are you using something in those files that would require namespace::std? If you dont #include any headers that would use that namespace, then the compiler will complain, as you've experienced first-hand.
Andrew Song
A: 

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.

Alan
I'd recommend doing the right thing. Don't put *any* usings in your header files, but be liberal in your cpp files to make the code easier to use
Orion Edwards
Putting a using namespace in the header file is just asking for trouble.
Martin York
at most, put a "using namespace_name::name;" into your source file. so you have control over exactly what you make visible, and if conflicts happen, they show up immediately at that line. any "using" in header files is a huge pain. most people and all "big playas" agree with that, i think.
Johannes Schaub - litb
A: 

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 )

Jay
A: 

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;
};
Artem Barger
This is definitely a way to answer the question Tony R asked, I don't think it's a good idea to pollute an external namespace.In general, unless you created the namespace you shouldn't add to it.
+1  A: 

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.