namespace question

Started by
5 comments, last by Zakwayda 16 years, 2 months ago
Whenever I write my programs I always use the following line after my #include lines. using namespace std; so whenever I use cout or cin I don't have to put std:: infront of it, is this bad practice? my book covered some stuff on it that said cout could be in more than one library or something of that sort so I should put the std:: infront of it. Should I stop doing this?
Advertisement
I think it is bad practice, it contradicts the point of the standard library being in the namespace to begin with. I always explcitly 'using' something. For example:

using std::cout;
It's generally only considered bad to do this inside other header files. Whether you use it inside source (e.g. *.cpp) files is typically a stylistic choice. The issue is that there is no unusing namespace command. So using in a header forces every other file that includes that header to be using the namespace also. In a source file it's only affecting that single translation unit.
I personally think that "using namespace" on a global level is bad practice. This after rebuilding our buildsystem at work and migrating into building all code in "batches" like this:

Batch.cpp

//------code---------
#include "file1.cpp"
#include "file2.cpp"
#include "file3.cpp"
etc

Globally "using namespace" has been giving me LOTS of pain and suffering ;)
Generally accepted view point:
Never apply using namespace XXX; in a header (.h) file.

My personal view point:
Only apply using namespace XXX; in an implementation (.cpp) file if:
a) The thing you wish to use has a lengthy list of namespaces.
b) You're a lazy, lazy person. [rolleyes]

Since "std::" isn't lengthy, if omitted, I'd classify it under the latter option.

As previously mentioned, it's a stylistic thing, but why be inconsistent between the header and implementation?
Quote:Original post by dmatter
a) The thing you wish to use has a lengthy list of namespaces.

If you're referring to long namespace names or nested namespaces, you can also just do namespace aliases instead. e.g.:

namespace fs = boost::filesystem;
Personally I never use using directives, either in .h or .cpp files. I sometimes use namespace aliases in .cpp files, and occasionally use using declarations in .cpp files or at function scope. (As noted above though, even in .cpp files, global using statements can cause problems.)

The issue of using statements in headers aside, it doesn't bother me at all to have to type a few extra characters (e.g. std::) here and there. In fact, I like being able to tell immediately where a particular type or function is drawn from (the SC++L, Boost, some third-party library). I think it makes the code clearer.

My advice would be to try writing your code without any using directives or declarations. If you find something that's just too verbose or awkward (e.g. boost::filesystem::'s all over the place), try a namespace alias.

All IMO.

This topic is closed to new replies.

Advertisement