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 name
A lot of code is written in the global namespace. If someone really redefines ::std::, it won't matter how you refer to it in that kind of code.
Also, your scenario is both unlikely and easy to work around. Firstly, the convention since there was a 'std' was that you don't use that name. Secondly, if you were to encounter a package that defined the 'std' namespace, you just do this:
#include <package_name.hpp>
namespace package_name {
using namespace std;
}
namespace my_app {
class my_class : public package_name::class_of_something {};
}
Or something similar. You might have to explicitly name ::std as std. My point is this: people go about using the un-anchored 'std::' all the time because even the worst-case consequences aren't a big deal, and unlikely at that.
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.
In the same way that names prefixed with _
are reserved, consider std::
as good as reserved.
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. :)
why do people always say std::
Not always. I use string
and using ::std::string
. So nothing bad if there will be fred::std::string
, because I still use std::string
. In small cpp-files it could be even using namespace ::std
. Sample:
#include <iostream>
using ::std::string;
//using namespace ::std; // good for small cpp-files
int main()
{
string s = "sometext"; // no leading ::std
}
you should just never name a namespace std
Yes, you shouldn't give a name std
to custom namespace.
In the case of std
I wouldn't assume any other namespace with the same name, same classes but different meaning.
In other cases, the leading ::
just looks ugly, and makes reading code more difficult (in my opinion). Namespace clashes are too rare to put the ::
in front of everything.