Should I use std::cout or have using namespace std; ?

Started by
7 comments, last by JohnnyCode 9 years, 3 months ago

Every tutorial I go to I see them all telling me to have "using namespace std;" at the beginning if I want to have words. But everyone else tells me to use std::cout. I do not know the difference between the 2. So if anyone could help me idk which is better or if it does not even matter. I would think that having namespace std; at the beginning would make things easier because then I just have to write cout << "text"; it's longer to type std::cout.

So which should I use?

Advertisement

using namespace std brings everything from the std namespace into the current scope. While this might be fine in individual functions, as a general rule you should avoid it in headers and often times in source files as well. Name collisions can make for significant confusion, and there are many common things in the standard library that are poorly named.

Fully qualifying names, such as using std:: ensures that you're not polluting the current name scope with names you might not necessarily be using, however at the same time it can add additional issues for automatic resolution of overloads, a good example being std::to_string and implementing your own to_string. Ideally you want it to simply pull in the correct overload without qualification.

Another option is to simply indicate the exact names you wish to bring into the current scope from another namespace. For example: using std::to_string which will bring the to_string function from the std namespace into the current name scope, and thus allow you to use it and your overloads seamlessly.

In the end I recommend avoiding using namespace std (and similar using namespace statements) whenever possible. Stick with either exact name qualification or narrow using statements which indicate exactly what names you want to bring into scope.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Elaborating on that a little, allowing using statements inside a header is probably the worst mistake in this case. It forces the 'using' on everyone that includes the header.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

Short version: Use using std::cout; at source level when appropriate, but avoid pulling in the whole namespace, or putting using statements in headers. Remember that using can be applied at function or class scope, not just file.

SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Also you should prefix c style functions if necessary that are not part of a namespace e.g. ::fopen()

This makes things explicitly clear what function you are referring to without having to scroll through the code and look for "using" statements.

This makes your code self documenting wich is always a plus...

I don't personally agree with avoiding using namespaces in .cpp files I see no problem with that, but yeah in header files it is not very professional. You're going to have source code that randomly includes namespaces, and then if you remove an include you need to add your using clause. It can also lead to strange build problems related to the order of your includes. Say you forgot to add a using clause to your header but the project builds because you always include if after a header that has the clause, but eventually someone removes the other header and it breaks the build. Header files should be independent entities that can be included in any order or any number of times. I know this rule makes things annoying if you have long C#-style namespaces like company::project::component and have to write code in your headers (templates, etc...) but this is in the interest of the other programmers' sanity! smile.png

The only exception I can think of is if say you are implementing a library with a single namespace. I still wouldn't do it but I guess I would tolerate adding a using namespace your_library in a precompiled header of the library since you're going to be using the namespace in all your source files anyway, but not in any other headers, and especially not in the public headers that you will distribute with your library.

I know this rule makes things annoying if you have long C#-style namespaces like company::project::component and have to write code in your headers (templates, etc...) but this is in the interest of the other programmers' sanity! smile.png


Insanely long namespaces can be abbreviated without pulling them into the global namespace as well:
namespace cpc = company::project::component
.

That said, I personally never ever apply any kind of using directive to std. It's short and adds a lot of clarity on a glance when you look at things. For the reasons already mentioned other using directives are completely out at header scope. What I will sometimes do is abbreviate the namespace as shown above (not at header scope either). Very occasionally I will add a using directive at function scope.


I don't personally agree with avoiding using namespaces in .cpp files I see no problem with that

Here's the problem with just inserting using namespace into source files: What can you tell me about this line of code:


sort(ptr, term);

Not a lot, unfortunately. You can probably infer that ptr is likely the start of the range we want to sort, but term.... could be anything. It could be the end of the range we want to sort, it could be a function pointer that is sorting the contents of ptr by "term." Without context it is hard to determine what this piece of code is actually doing. We know it's sorting (presumably) because of the name. But we know very little about the sort function (what kind of a sort is it?), how it does its comparisons (is term a function pointer? The terminal to stop sorting when found? The end pointer of the range? A pointer to the last element to be sorted?), what kind of a comparison function does it use if term is some sort of a terminal?

On the other hand, if you see:


std::sort(ptr, term);

you immediately know several things...

1. You know that ptr and term are iterators of a random access type (most likely pointers) to the first element of the range, and one past the end of the range you want sorted.

2. You know the sorting performance characteristics of std::sort (N log2N)

3. You know that by default it will use std::less as its comparison predicate, and that said comparison predicate uses the < operator for comparison.

All of that extra clarity for 5 extra characters. I'll take that over using namespace std; or similar in a source file at any time.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Not applying using namespace has severall advantages

1- instant readiblity of code

2- great auto-finish comfort

3- library/modules tracking

4- lesser compile time duties

and the only disadvantage

5- you have to type more

but the disadvantage is greatly negated by auto-finish higher comfort. If you happen to be leaning towards using "using", like if you are aware of 9 mentions of the namespace incoming (rare situation), you still should avoid it, but that is just my personal rule I apply to myself.

This topic is closed to new replies.

Advertisement