Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I wrap all my c++ code in its own namespace?

Tags:

c++

namespaces

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?

like image 329
Abtin Forouzandeh Avatar asked Feb 02 '09 07:02

Abtin Forouzandeh


People also ask

Why is creating a namespace around your code useful?

Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.

Does C support namespace?

Namespaces provide the space where we can define or declare identifiers i.e. names of variables, methods, classes, etc. Namespace is a feature added in C++ and is not present in C.


3 Answers

Many C++ developers do not use namespaces, sadly. When I started with C++, I didn't use them for a long time, until I came 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 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.

like image 130
Johannes Schaub - litb Avatar answered Sep 20 '22 22:09

Johannes Schaub - litb


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.

like image 41
Robert Gould Avatar answered Sep 20 '22 22:09

Robert Gould


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.
like image 38
Abtin Forouzandeh Avatar answered Sep 18 '22 22:09

Abtin Forouzandeh