tags:

views:

1004

answers:

8

I come from a c# background where everything has its own namespace, but this practice appears to be uncommon in the c++ world. Should I wrap my code in it's own namespace, the unnamed namespace, or no namespace?

+2  A: 

You only really need namespaces if there's a risk of names conflict - some globally seen function or variable or class is defined more than once. Otherwise you'll do just fine with no namespace - just name your classes so that they don't duplicate runtime library classes and make global functions/variable to be static members of some reasonable classes.

sharptooth
+7  A: 

Depends, if your code is library code, please wrap it in namespaces, that is the practice in C++. If your code is only a very simple application that doesn't interact with anything else, like a hello world sort of app, there is no need for namespaces, because its redundant.

And since namespaces aren't required the code snippets and examples on the web rarely use them but most real projects do use them.

Robert Gould
+1  A: 

It really depends upon whether you expect there to be any conflicts.

Two scenarios;

1) If you are creating code that may be used by others (e.g libraries) then there could be namespace clashes so using your own namespace is a good idea.

2) If you are using third-party libraries their code may not be namespaced and could conflict with yours.

I would also say that if you expect your code to be sizable and cover many different areas (math/physics/rendering) then using namespaces does make the code easier to grok, particularly for types that are not obviously classified.

Andrew Grant
Your second point is especially true when incorporating C libraries, as they lack namespaces.
Robert Gould
@Robert - Yes, good point.
Andrew Grant
Nothing "especially true" for C libraries here. Good C libraries are namespaced via prefixes. Good С++ libraries are namespaced via namespaces. Bad C and C++ libraries are not namespaced. It's a matter of quality, not language.
Constantin
+25  A: 

Many C++ developers do not use namespaces, sadly. When i started with C++, i didn't use them for a long time, until i've come to the conclusion that i can do better using namespaces.

Many libraries work around namespaces by putting prefixes before names. For example, wxWidgets puts the characters "wx" before everything. Qt puts "Q" before everything. It's nothing really wrong with that, but it requires you to type that prefix all over again, even though when it can be deduced from the context which declarations you mean. Namespaces have a hierarchic order. Names that are lexically closer to the point that reference them are found earlier. So if you reference "Window" within your GUI framework, it will find "my::gui::Window", instead of "::Window".

Namespaces enable some nice features that can't be used without them. For example, if you put your class into a namespace, you can define free functions within that namespace. You then call call the function without putting the namespace in front by importing all names, or selectively only some of them into the current scope ("using declaration").

Nowadays, i don't do any project anymore without using them. They make it so easy not to type the same prefix all over again, but still have good organization and avoidance of name-pollution of the global namespace.

Johannes Schaub - litb
Yeah, and it makes the code MUCH more clear. A global variable tells you nothing, but config::max_threads at least gives you a clue about where it's coming from.
Andrei Taranchenko
+1  A: 

but this practice appears to be uncommon in the c++ world

Really. All the code I see seems to be wrapped in a namespace.
I use the same type of convention I see in Java (except I drop the com).

In Java

package com.<Company>.<Product>.<Package>;

In C++

namespace <Company>
{
     namespace <Product>
     {
         namespace <Package>
         {
         }
     }
 }
Martin York
+2  A: 

We had problems wrapping code in managed C++ that uses our common libraries here.

Some classes had the same names as System class in the .NET library (i.e. System.Console).
We had to do some ugly macro patches to workaround these problems.

Using namespaces at the beginning would have prevented this.

total
+2  A: 

I just discovered Google's c++ style guide and they have namespace guidelines.

The whole guide is worth reading, but to summarize, they say:

  • Add unnamed namespaces to .cc files, but not .h files.
  • Wrap entire (after includes/declarations) .cc and .h files in named namespaces.
  • Namespaces do not increment the indent level.
  • At the closing brace for a namespace write } // namespace.
  • Don't declare anything in std, because it is undefined.
  • using the using directive is forbidden.
  • the using declaration is allowed in functions, methods, and classes.
  • namespace aliases are allowed anywhere.
Abtin Forouzandeh
Why not add unamed namespaces to .h files?
Casebash
+1  A: 

I'd say that it's a good idea, if for no other reason than to keep your stuff from getting stepped on when mixed with other third-party code.

I try to go even farther than that by putting as many variables and functions as I can into classes. But that's sometimes harder to do than it should be (compared to other OO languages).

Loadmaster