tags:

views:

279

answers:

10
+2  Q: 

Namespaces and C++

I have observed that in C++ namespaces are very rarely used while in .Net it is very common. Is there any particular reason for this.

Also i would like to know if others have seen namespaces being used commonly in C++.

EDIT: Actually i am referring to custom applications and not standard libraries like STL or any other thing.

A: 

The Boost C++ libraries use namespaces extensively.

Sam Miller
Why the down vote?
Sam Miller
+3  A: 

I have observed that in C++ namespaces are very rarely used

Would you be able to back that up with relevant empirics? Boost, Qt, the STL, ... all use namespaces.

Dirk Eddelbuettel
Many, many names in Qt are in the global namespace...
jpalecek
@Jpalecek: Yet a lot are not. The point being made is that Qt uses namespaces extensively.
Martin York
@Martin York: I did a `nm -DC /usr/lib/libQtCore.so` on my Debian system, and looked for names not in the global namespace. There are none, the same applies for `libQtGui.so` and `libQtNetwork.so`. This is not what I call "extensive use". In fact, Qt API contains very few namespaces in rather secondary areas, plus there are some namespaces as implementation details.
jpalecek
@jpalecek: Check out the documentation: http://cep.xor.aps.anl.gov/software/qt4-x11-4.2.2-browser/d8/d50/namespace_qt.html
Martin York
@Martin York: Yes, this is a _single_ namespace used for constants. Basically, you won't come across a different namespace when using Qt regularly. This doesn't constitute extensive use, especially when the global namespace is still polluted with names like `QTextBox` and `qSort` (note the Q plain-old-prefix-seminamespace).
jpalecek
@Martin York: QT is completely within the namespace QT: This is just false. Even the documentation you link states it, so you should consider more what you call "silly". qSort is this: http://cep.xor.aps.anl.gov/software/qt4-x11-4.2.2-browser/d4/d35/src_2corelib_2tools_2qalgorithms_8h.html#8f9edc5e7400d5c3d8075f490881dd7d, not a C qsort. `QTextBox` should have been `QTextEdit`.
jpalecek
@jpalecek: Your attempt to twist the meaning of extensive is just silly: http://encarta.msn.com/dictionary_1861609950/extensive.html QT is mostly within namespace (http://cep.xor.aps.anl.gov/software/qt4-x11-4.2.2-browser/namespaces.html) (thus the use is extensive (as in broad of scope)). You are obviously confusing C runtime qsort (in the global namespace) with qSort from QT which is not in the global namespace (as linked to by the documentation you provided). QtextEdit does seem to be in the global namespace but I am not sure how you justify that making the usage less extensive.
Martin York
+1  A: 

I have seen them used very commonly, as they should be. What is your sample of namespaces not being used?

inflagranti
A: 

From working at 5 different places I can say that 2 of the 5 used namespaces before I joined.

I guess that namespaces in C++ doesn't get used as much (as they should?) is due to the syntax being cludgy.

ie: I want to write this:

namespace xml::xmpp::core { }

But instead have to write this:

namespace xml { namespace xmpp { namespace core { } } }

And to forward declare, I would like to write:

class xml::parser;

But have to write

namespace xml { class parser; }

But I can write:

using namespace xml::xmpp::core;

On a slightly different note, can anyone tell my why classes have to be terminated by a semi-colon but anything else (functions, namespaces) doesn't?

graham.reeds
That last deserves its own question, but it's because you can declare a variable. There are two optional parts to a class declaration, the definition (if omitted, you have a forward declaration), and the list of variables. Have a look at some old C code that typedefs every struct to see what I mean.
Ben Voigt
okay. It does bug me though.
graham.reeds
namespace xml { class parser; }class xml { class parser { ... }; };class xml::parser; // Which xml::parser is this predeclaring?
Logan Capaldo
That's why you have rules that take away the confusion. I would have namespace take precedent over class names, so xml::parser would be namespace::class. There are probably problems with that but I haven't sat and thought long and hard about it.
graham.reeds
+6  A: 

In C++, namespaces were added to the language some time after the initial implementation, so there was much application code that didn't use them. So, also due to their late addition, all of the standard library was put into one namespace. Hence with a simple using namespace std; you could pretty much ignore namespaces if you wanted to.

In C#, on the other hand, namespaces were involved from the very start, and the library was divided out over a large number of them. Further, the Wizards everyone uses to create their initial code, put the classes into a namespace by default. This forced a much greater awareness of namespaces.

James Curran
+4  A: 

Namespaces in C++ are not the same at all than in .Net, ActionScript and Java (that shares the same concept). They are not the same concept at all.

In C++, namespaces are primarly there to allow encapsulation of several types and functions in a named context, the namespace. It's only about naming and accessing names.

In .Net, ActionScript and Java, namespaces are more about modules than names. They forces the developper to organize his code in separate namespaces that are each about one purpose, context. As those languages are dynamic (instead of static like C++), the namespaces allow late binding of types to code, making compilation fast because you only have to have the canonical name (namespace + name) of the type you want to use in a file.

In C++ there is no module comcept, only compilation units that don't know at all about each other.

Now about their usage, it's often good practice to use a namespace in C++ to encapsulate a module (executable or dll/so), some implementation code or any useful sub part of code. That said most of the time it's better to not have a too deep hierarchy of namespaces. For historical reasons, a lot of C++ developers didn't even knew for a very long time that C++ had a feature named namespace. Now what we call "modern C++" suggest that you know about it, but a lot of old C++ code is still used today and maybe that's what you're looking at when talking about namespaces.

Namespaces in C++ being a different feature than in other languages makes also it's writing more or less apparent in code. In fact, it's so different that you'll not manage it the same way at all, making code really hard to understand when you're coming from say .Net. So you shouldn't take namespaces the same way between languages, they are really different concepts.

Klaim
"In .Net, ActionScript and Java, namespaces are more about modules than names." This is definitely inaccurate with regards to .net. Members of a namespace can be defined in multiple assemblies, and an assembly can contain definitions in multiple namespaces. This is in fact, very similar to how namespaces work in C++. Java is slightly different as the namespaces inform file system locations of .class files by default (ignoring jars for the moment) but even then, the module unit is the .class, not the namespace and different "vendors" can provide classes in the same namespace.
Logan Capaldo
"modules" here is not related directly to assembly or .jar but to "systems" - a class, several class, a jar etc. . That said, thanks for the precision.
Klaim
+2  A: 

The simplest answer would be that when you start a program in Visual Studio (2003 and onward) it creates namespaces by default. Other (older) IDEs do not, I suppose.

.NET has encouraged it from the start. Namespaces weren't always part of the original implementation in C++. It has gained traction with .Net.

0A0D
A: 

Namespaces are your best friend in big projects !

Seen it or not - doesn't matter. Embrace the habit to use them.

Poni
A: 

As time passes I see more and more c++ codes making thoughtful use of namespaces. You may have been observing the lingering effects of the c-with-classes programming style...

Mind you I'm a physicist and most of the code I look at was written by other physicists, so this might be an artifact of my domain.

dmckee
A: 

One reason is that some popular compilers (gcc in particular) added support for namespaces pretty late, and to be more portable many projects were started without namespaces. Of course, for a new project not using namespaces would be strange.

Nemanja Trifunovic