Namespaces and C ++

I noticed that in C ++ namespaces are very rarely used, and in .Net it is very common. Is there any special reason for this?

I would also like to know if other namespaces commonly used in C ++ have been seen.

EDIT: Actually, I mean user applications, not standard libraries like STL or any other things.

+6
c ++ namespaces
source share
9 answers

In C ++, namespaces were added to the language some time after the initial implementation, so there was a lot of application code that didn't use them. Thus, also because of their late addition, the entire standard library was placed in one namespace. Therefore, with a simple using namespace std; you could ignore namespaces if you want.

In C #, on the other hand, namespaces were involved from the very beginning, and the library was divided into a large number of them. In addition, everyone uses Wizards to create their own source code by placing classes in the default namespace. This has led to significant awareness of namespaces.

+10
source share

Namespaces in C ++ are not the same as in .Net, ActionScript, and Java (which share the same concept). They are not at all the essence.

In C ++, namespaces are where encapsulation of several types and functions is required in a named context - a namespace. This applies only to names and access to names.

In .Net, ActionScript, and Java, namespaces are more about modules than about names. They force the developer to develop their code in separate namespaces, each of which is associated with one goal, context. Since these languages ​​are dynamic (instead of static, like C ++), namespaces allow late type binding to code, which makes compilation fast because you only need to have the canonical name (namespace + name) of the type that you want to use in file

In C ++, there is no module command, only compilation units that do not know about each other at all.

Now about their use, it is often useful to use a namespace in C ++ to encapsulate a module (executable or dll / so), some implementation code, or any useful part of the code. However, most of the time it’s better not to have a too deep hierarchy of namespaces. For historical reasons, many C ++ developers did not even know that C ++ has a function called namespace. Now what we call "modern C ++" suggests that you know about it, but a lot of the old C ++ code is still in use today and maybe this is what you look at when you talk about spaces names.

C ++ namespaces, which are another function different from other languages, also make the record more or less obvious in the code. Actually, it is so different that you cannot handle it the same way, generally speaking, it is very difficult to understand the code when you say .Net. Thus, you should not take namespaces the same way between languages, these are really different concepts.

+7
source share

I noticed that C ++ namespaces are very rarely used

Could you support this with appropriate empiricists? Boost , Qt , STL, ... all use namespaces.

+3
source share

The simplest answer is that when you run the program in Visual Studio (2003 onwards), it creates namespaces by default. There are no other (older) IDEs, I suppose.

.NET encouraged it from the start. Namespaces were not always part of the original C ++ implementation. He acquired a connection with .Net.

+2
source share

From working in 5 different places, I can say that 2 out of 5 used namespaces before I joined.

I assume that namespaces in C ++ are not used as much (as they should?) Due to the syntax being clumsy.

ie: I want to write this:

namespace xml::xmpp::core { }

But instead you need to write this:

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

And to send an ad, I would like to write:

class xml::parser;

But you need to write

namespace xml { class parser; }

But I can write:

using namespace xml::xmpp::core;

In a slightly different note, can anyone say why classes should be completed with a colon, but there is nothing else (a function, namespace)?

0
source share

Namespaces are your best friend in large projects!

Saw it or not - it does not matter. Declare the habit of using them.

0
source share

Over time, I see more and more C ++ codes, thoughtful use of namespaces. You may have seen the c-with-classes programming style delay effects ...

Keep in mind that I'm a physicist, and most of the code I'm looking at was written by other physicists, so this could be an artifact of my domain.

0
source share

One reason is that some popular compilers (especially gcc) added namespace support quite late, and to be more portable, many projects started without namespaces. Of course, for a new project that does not use a namespace, it would be strange.

0
source share

Boost C ++ libraries make extensive use of namespaces.

-one
source share

All Articles