views:

712

answers:

8

When using C++ namespaces, do you prefer to explicitly name them, like this:

std::cout << "Hello, world!\n";

Or do you prefer using namespace:

using namespace std;
cout << "Hello, world!\n";

And if if you prefer the latter, do you declare your usings at file or function scope?

Personally I prefer to explicitly name them - it's more typing but when using a mixture of namespaces (e.g. std and boost) I find it more readable.

+13  A: 

I always use using namespace for std & boost. Everything else I tend to use an explicit namespace unless it is used so much that it would clutter up the code.

In headers, I never use using namespace to avoid polluting the global namespace of the #including source.

Ferruccio
+2  A: 

I only use explicit namespaces when there's some ambiguity. It is more readable, but the extra typing is too tedious, and you have to assume other developers have a baseline level of familiarity with standard libraries.

The only other times I spell out a namespace are when I'm only using it once or twice, like adding in a quick debug statement, or if I'm using some non-standard library.

I typically declare the namespace at file scope, but if you're mixing namespaces it might make sense to put the declaration closer to the point where it's used, in function scope.

Bill the Lizard
+12  A: 

I use always explicit ones. Writing std does not hurt me and I clearly see where is it from. It's useful when you have some legacy project to take care of with it's own "strings", "vectors" etc to maintain. The more information the code carries with it, the better.

Nazgob
It doesn't hurt until there is a template specialization that you miss out on because you specified the namespace explicitly. I must admit I do the same out of habbit, but it is something I think about more conciously since reading Meyer's "Effective C++".
ceretullis
+4  A: 

My general rule is always explicitly use the namespace in headers, and usually use using in the code. The reason for the former is to make it explicitly clear in every part of the definition what is being used, and the reason for the latter is that it makes it easy to use replacements from another namespace if that becomes necessary. i.e. if we want to start using foo::string instead of std::string we just need to update the header and the using statement rather than replacing every instance of std::string with foo::string in the code.

Of course, that is less useful for classes that reside in the std:: namespace, since even if you replace one class you're still likely to use others in std, and may run into ambiguity issues, but that was just an example.

Gerald
+9  A: 

Extra typing is not the issue here. The problem with explicitly qualified names is the visual clutter. Let's face it, C++ syntax is untidy. No need to make this worse by needlessly making names longer and sprinkling the code generously with ::s.

I'm with Jeff Atwood: The Best Code is No Code At All. This is so true.

Namespace imports are a great way of reducing clutter with no drawback: As long as the scope of opened namespaces is reduced to a single compilation unit1, name conflicts, should they appear, can be resolved easily.

Why explicit names should (in general) be more readable has always been a mystery to me. The readers should generally know the code good enough to be able to deduce semantics. If they aren't, the code needs fixing anyway.


1) Corollary: no using in headers!

Konrad Rudolph
+1  A: 

using at function scope, or if the function is very small (often is), just explicit namespace

Nemanja Trifunovic
+1  A: 

using and using namespace are Very Very useful to render the code more readable - remove clutter.

But in any case where it makes it harder to find out where a symbol comes from, I refuse importing it's whole namespace.

I try to limiit the scope of the imported namespaces:

void bar() {

   // do stuff without vector

   { using std::vector;
      // do stuff with vector
   }

   // do stuff without vector
}

For "generally known" libraries, like std, I would dare using using namespace std. There is reason to believe everyone reading this code knows these symbols.

As a sidenote, the using keyword is also used to indicate that a derived class also exports the overloaded members of its superclass.

class A {
  void f( A  );
  void f( bool );
};

class B : public A {
  using A::f; // without this, we get a compilation error in foo()
  void f(bool);
};

void foo() {
  B b;
  b.f( A() ); // here's a compilation error when no `using` is used in B
}
xtofl
+2  A: 

I tend to explicitly import the names that I need at the top of the .cpp file, so...

using std::cout; using std::endl;

etc...

That way I have control over the names that I use and it's easy to see where they come from and the code isn't cluttered at the point of use.

In the rare cases where I am using two names from different namespaces I fully qualify them at the point of use.

I always use fully qualified names in headers and hardly ever use 'using namespace x' anywhere...

Len Holgate