views:

5616

answers:

14

Okay, sorry for the simplistic question, but this has been bugging me ever since I finished high school C++ last year. I've been told by others on numerous occasions that my teacher was wrong in saying that we should have "using namespace std;" in our programs, and that std::cout and std::cin are more proper. However, they would always be vague as to why this is a bad practice.

So, I'm asking now: Why is "using namespace std;" considered bad? Is it really that inefficient, or risk declaring ambiguous vars(variables that share the same name as a function in std namespace) that much? Or does this impact program performance noticeably as you get into writing larger applications?

I'm sorry if this is something I should have googled to solve; I figured it would be nice to have this question on here regardless in case anyone else was wondering.

A: 

IMHO, It's not bad.
Some people prefer to use it, but some (as I) not.

Dmitriy
technically, nothing is bad.
MathGladiator
I exist. Pigs exist. Therefore, I am... a pig? No, that's not right at all...
Matthew Scharley
its use is not mandatory, it is only a recommendation of best practices.
lsalamon
A: 

The only reason I can think of is surprise.

If I see cout << blah, instead of std::cout << blah

I think what is this 'cout'? Is it the normal cout? Is it somethign special?

Martin Beckett
(as a side note, you can use backticks to surround code in normal text, to get this: `cout << blah`)
Greg Hewgill
+45  A: 

This is not related to performance at all. But consider this: You are using two libraries called Foo and Bar:

using namespace foo;
using namespace bar;

Everything works fine, you can call Blah() from Foo and Quux() from Bar without problems. But one day you upgrade to a new version of Foo 2.0, which now offers a function called Quux(). Now you've got a conflict: Both Foo 2.0 and Bar import Quux() into your global namespace. This is going to take some effort to fix, especially if the function parameters happen to match.

If you have used foo::Blah() and bar::Quux() then the introduction of foo::Quux() would have been a non-event.

Greg Hewgill
Thanks. I had never considered that possibility.
Mana
I've always liked Python's "import big_honkin_name as bhn" so you can then just use "bhn.something" rather than "big_honkin_name.something"- really cuts down on the typing. Does C++ have something like that?
paxdiablo
@Pax namespace io = boost::filesystem;
AraK
You could declare a namespace (short) into which you 'use namespace big_honking_name', then you'd be able to short.something.
Michael Kohne
Cool. Of course I haven't touched C++ since before the birth of namespaces, but it's nice to know it's there.
paxdiablo
I think it's overstating things to say it's "some effort to fix". You'll have no instances of the new foo::Quux so just disambiguate all your current uses with bar::Quux.
MattyT
Would any sensible person create a library with types whose unqualified name collide with the std types?
erikkallen
@MattyT: Things can get much worse than "having to fix". (see my answer: http://stackoverflow.com/questions/1452721/1453605#1453605).
sbi
@erikkallen: That the std lib has taken hundreds (or even thousands) of names, many of which are very popular and common (`error`, `list`, `sort`), was, IIRC, an important reason for putting it into its own namespace.
sbi
To replicate the python "import longlongnamespace as spc" behaviour you can simply use a #define. I don't see anything wrong with that.
TomA
@TomA: The problem with `#define` is that it doesn't restrict itself to namespaces, but tramples over the whole code base. A namespace alias is what you want.
sbi
For the record, a namespace alias is `using namespace bhn = big_honkin_name;` as opposed to #defines or a using statement within a new namespace.
AshleysBrain
+1  A: 

It's all about managing complexity. Using the namespace will pull things in that you don't want, and thus possibly make it harder to debug (I say possibly). Using sdt:: all over the place is harder to read (more text and all that).

Horses for courses - manage your complexity how you best can and feel able.

Preet Sangha
+1  A: 

It depends on where it is located. If it is a common header, then you are diminishing the value of the namespace by merging the into the global. Keep in mind, this could be a neat way of making module globals.

MathGladiator
+3  A: 

Consider

// myHeader.h
using <sstream>
using namespace std;


// someoneElses.cpp/h
#include "myHeader.h"

class stringstream {  // uh oh
};

Note that this is a simple example, if you have files with 20 includes and other imports you'll have a ton of dependencies to go through to figure out the problem. The worse thing about it is that you can get unrelated errors in other modules depending on the definitions that conflict.

It's not horrible but you'll save yourself headaches by not using it in header files or the global namespace. It's probably alright to do it in very limited scopes but I've never had a problem typing the extra 5 characters to clarify where my functions are coming from.

Ron Warholic
+27  A: 

I think it's bad to put it in the header files of your classes: because then you would be forcing anyone who wants to use your classes (by including your header files) to also be 'using' (i.e. seeing everything in) those other namespaces.

However, you may feel free to put a using statement in your (private) *.cpp files.

ChrisW
This is also what I have been taught. +1
Default
+6  A: 

If you import the right header files you suddenly have names like hex, left, plus or count in your global scope. This might be surprising if you are not aware that std:: contains these names. If you also try to use these names locally it can lead to quite some confusion.

If all the STL stuff is in it's own namespace you don't have to worry about name collisions with your code or other libraries.

sth
A: 

I do not think it is necessarily bad practice under all conditions, but you need to be careful when you use it. If you're writing a library, you probably should use the scope resolution operators with the namespace to keep your library from butting heads with other libraries. For application level code, I don't see anything wrong with it.

Dr. Watson
+2  A: 
  1. you need to be able to read code written by people who have different style and best practices opinions than you.

  2. If you're only using cout, nobody gets confused. But when you have lots of namespaces flying around and you see this class and you aren't exactly sure what it does, having the namespace explicit acts as a comment of sorts. You can see at first glance, 'oh, this is a filesystem operation' or 'thats doing network stuff'.

Dustin Getz
+11  A: 

I agree with everything Greg wrote, but I'd like to add: It can even get worse than this! Foo 2.0 could introduce a function Quux() that is an unambiguously better match for some of your calls too Quux() than the bar::Quux() your code called for years. Then your code still compiles, but silently calls the wrong function and does god-knows-what. That's about as bad as things can get.

Keep in mind that the std namespace has tons of identifiers, many of which are very common ones (think list, sort, string, iterator etc.) which are very likely to appear in other code, too.


Here's one more data point: Many, many years ago, I also used to find it annoying having to prefix everything from the standard library with std::. Then I worked in a project where it was decided at the start that both using directives and declarations are banned except for function scopes. Guess what? It took most of us very few weeks to get to used to write the prefix and after a few more weeks most of us even agreed that it actually made the code more readable. (There's a reason for that: Whether you like shorter or longer prose is subjective, but the prefixes objectively add clarity to the code. Not only the compiler, but you, too, find it easier to see which identifier is referred to.)

In almost a decade, that project grew to have several million lines of code. Since these discussions comes up again and again, I once was curious how often the (allowed) function-scope using actually was used in the project. I grep'd the sources for it and only found one or two dozen places where it was used. To me this indicates that, once tried, developers didn't find std:: painful enough to employ using directives even once every 100kLoC even where it was allowed to be used.


Bottom line: Explicitly prefixing everything doesn't do any harm, takes very little getting used to, and has objective advantages. In particular, it makes the code easier to interpret by the compiler and by human readers - and that should probably be the main goal when writing code.

sbi
It does significantly harm the density of code you can pack in a single line. You end up writing your code in a very long-winded way; which reduces readability. Personally, I think shorter (but not too short) code tends to be more readable (since there is less stuff to read, and less stuff to get distracted about).
Lie Ryan
@Lie: Whether you like short or long prose is a _subjective preference_. (I have met developers who actually liked long identifiers, even though it meant they had to split their lines for even the simplest function calls.) Whether you see exactly which identifier, OTOH, is referred to is an _objective criterion_. FWIW, _I used to share your POV_, was forced to taste the other side, and never looked back.
sbi
@sbi: While I do not disagree that for non-std:: identifiers, being able to see where a name comes from can improve readability. I think the std:: denotation is just superfluous. Despite being an objective criterion, it has superficial value since you should expect any competent C++ programmer to know C++ standard libraries; and you'd expect no sane programmers would deliberately clash names with a standard library names. Personally, I put `using namespace std` on all of my code; though I use other namespaces conservatively, depending on frequency of usage.
Lie Ryan
Guess you missed out on the old days before C++ had a standard `string` class, and seemingly every library had their own. Tell you what: We'll keep writing our code with `std::`, and you can run our code through `grep -v std:: | vim` when you're browsing it. Or you can teach your editor that `std::` is a keyword which is to be colored the same as the background color. Whatever works.
Mike DeSimone
@Lie: "I think the `std::` denotation is just superfluous." Your main argument against typing out full identifiers, that they add to identifier length, will hardly apply to `std::`. But even so, I disagree. See [this problem](http://stackoverflow.com/questions/2712076/how-to-use-iterator-in-c/2712125#2712125) why.
sbi
Lie Ryan
@Lie: "The problem you mentioned, is because someone named a function with the same name as a standard library function" And that's exactly the problem! How do you propose to avoid that? In the `std` namespace are _hundreds_ (if not _thousands_) of identifiers, among them _very_ popular ones. There are so many, you simply can't avoid clashing with them. And that's why they are all in `std`. Add `using namespace std` to your files, and sooner or later you run into such problems.
sbi
I don't think `std::` is harmful at all. It carries very important information (namely "whatever comes after is part of the standard library", and it is still a pretty short and compact prefix. Most of the time, it's no problem at all. Sometimes, you have a few lines of code where you need to refer to specific symbols in the `std` namespace a lot, and then a `using` statement in that particular scope solves the problem nicely. But in the general case, it's not noise, it conveys valuable information *in addition* to removing ambiguities.
jalf
@sbi, @jalf: It uglifies your code, and I'm not going to take "no" for that.
Lie Ryan
@Lie: If you don't want to be persuaded by rational arguments, then why do you argue?
sbi
Lie Ryan
+4  A: 

I also consider it a bad practice. Why? Just one day I thought that function of a namespace is to divide stuff so I shouldn't spoil it with throwing everything into one global bag. However, if I often use 'cout' and 'cin', I write: using std::cout; using std::cin; in cpp file (never in header file as it propagates with #include). I think that noone sane will ever name a stream cout or cin. ;)

Yelonek
+4  A: 

One shouldn't use using directive at global scope, especially in headers. However there are situations where it is appropriate even in a header file:

template <typename FloatType> inline
FloatType compute_something(FloatType x)
{
    using namespace std; //no problem since scope is limited
    return exp(x) * (sin(x) - cos(x * 2) + sin(x * 3) - cos(x * 4));
}

This is better than explicit qualification (std::sin, std::cos...) because it is shorter and has the ability to work with user defined floating point types (via Argument Dependent Lookup).

robson3.14
+1  A: 

I recently ran into a complaint about VS 2010. It turned out that pretty much all the source files had these two lines:

using namespace std;
using namespace boost;

A lot of Boost features are going into the C++0x standard, and VS2010 has a lot of C++0x features, so suddenly these programs were not compiling.

Therefore, avoiding using namespace X; is a form of future-proofing, a way of making sure a change to the libraries and/or header files in use is not going to break a program.

David Thornley