Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does everybody use unanchored namespace declarations (i.e. std:: not ::std::)?

Tags:

c++

namespaces

It seems to me that using unanchored namespaces is just asking for trouble later when someone puts in a new namespace that happens to have the same name as a root level namespace and mysteriously alters the meaning of a whole lot of programs. So, why do people always say std:: instead of ::std::. Do they really mean to be saying "I want to use whatever std is handy, not the root one."?

Here is an example of what I mean:

In fred/Foo.h:

#include <string>  namespace fred {  class Foo {  public:    void aPublicMember(const std::string &s); };  } // end namespace fred 

In fred/Bar.h:

namespace fred { namespace std {  // A standard fred component  class string { // Something rather unlike the ::std::string    // ... };  } // namespace std  class Bar {  public:    void aPublicMember(std::string &s); };  } // namespace fred 

In oops.cpp:

#include <string> #include "fred/Bar.h" #include "fred/Foo.h"  // Oops, the meaning of Foo is now different. 

Is that what people want, or am I missing something?

And maybe you say that you should just never name a namespace std. And that's all well and good, but what about some other root level namespace then? Should any root level namespace anybody ever defines anywhere always be off-limits for a sub-namespace name?

To clarify, I won't consider any answer that tells me std is special because I just used it as an example. I'm talking about a general issue, and I'm using std as a prop to illustrate it, though I do admit it's a rather startling prop.

like image 815
Omnifarious Avatar asked Nov 02 '09 15:11

Omnifarious


People also ask

Why do people not use using namespace std?

It is considered "bad" only when used globally. Because: You clutter the namespace you are programming in. Readers will have difficulty seeing where a particular identifier comes from, when you use many using namespace xyz; .

Why we should not use using namespace std in C++?

While this practice is okay for example code, pulling in the entire std namespace into the global namespace is not good as it defeats the purpose of namespaces and can lead to name collisions. This situation is called namespace pollution.

What is a namespace why would you choose to use a namespace other than standard?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. 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.

What does the using std :: do for us and why would we use this instead of using std namespace?

“std” is an abbreviation for standard. So that means we use all the things with in “std” namespace. If we don't want to use this line of code, we can use the things in this namespace like this. std::cout, std::endl.


2 Answers

So, why do people always say std:: instead of ::std::

Probably because they never really had problems with ambiguity because of it.

Along the same lines: I never had to include "earth" in my address, and I'm not going to.

You have to draw the line somewhere, and this it is a reasonable assumption that others won't make their own std namespaces, or at least that a library that does won't be very popular. :)

like image 105
Mattias Nilsson Avatar answered Oct 05 '22 03:10

Mattias Nilsson


The practical reason for unanchored namespaces is that one level of namespaces usually is enough. When it isn't, a second level is usually going to be used for implementation details. And finally, even when using multiple levels, they are still usually specified implicitly from root level. ie. even inside namespace ns1, you'd typically refer to ns1::ns2::foo instead of ns2::foo or ::ns1::ns2::foo.

So, for these three reasons the ::ns1 form is redundant in normal cases. The only case where I'd consider it would be in submissions to Boost, because as a Boost author I won't know where my software will be used.

like image 39
MSalters Avatar answered Oct 05 '22 02:10

MSalters