Is it legal to replace something like this:
namespace foo { namespace bar { baz(); } }
with something like this:
namespace foo::bar { baz(); }
?
You can have the same name defined in two different namespaces, but if that is true, then you can only use one of those namespaces at a time. However, this does not mean you cannot use the two namespace in the same program. You can use them each at different times in the same program.
A namespace is a logical container in which all the names are unique; that is, a name can appear in multiple namespaces but cannot appear twice in the same namespace. A namespace is, literally, a space in which to store some names.
In addition, C also partitions a program's identifiers into four namespaces. Identifiers in one namespace, are also considered different from identifiers in another.
Absolutely nothing, as far as you avoid to use the using namespace <XXX>; directive. As David Vandevoorde said, the “fully qualified name” (the standard term for him “complete name”) is different, while you have to prefix the name with, <namespace>::, and compiler can make the difference between both.
You can combine namespaces into one name and use the new name (i.e. Foobar).
namespace Foo { namespace Bar { void some_func() { printf("Hello World."); } }} namespace Foobar = Foo::Bar; int main() { Foobar::some_func(); }
Pre C++17:
No, it's not. Instead of a bunch of indented nested namespaces, it's certainly valid to put them on the same line:
namespace Foo { namespace Bar { namespace YetAnother { // do something fancy } } } // end Foo::Bar::YetAnother namespace
C++17 Update:
You can now nest namespaces more cleanly in C++17:
namespace Foo::Bar::YetAnother { // do something even fancier! }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With