ckv
ckv

Reputation: 10838

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.

Upvotes: 7

Views: 1412

Answers (9)

Nemanja Trifunovic
Nemanja Trifunovic

Reputation: 24561

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.

Upvotes: 1

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.

Upvotes: 0

Poni
Poni

Reputation: 11327

Namespaces are your best friend in big projects !

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

Upvotes: 0

graham.reeds
graham.reeds

Reputation: 16486

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?

Upvotes: 0

user195488
user195488

Reputation:

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.

Upvotes: 2

Klaim
Klaim

Reputation: 69772

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.

Upvotes: 7

James Curran
James Curran

Reputation: 103555

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.

Upvotes: 11

Dirk is no longer here
Dirk is no longer here

Reputation: 368509

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.

Upvotes: 3

Sam Miller
Sam Miller

Reputation: 24174

The Boost C++ libraries use namespaces extensively.

Upvotes: -1

Related Questions