Jump to content
  • Advertisement
Sign in to follow this  
zell901

namespace question

This topic is 3793 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ;)

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!