Is it legal to replace something like this:
namespace foo {
namespace bar {
baz();
}
}
with something like this:
namespace foo::bar {
baz();
}
?
Is it legal to replace something like this:
namespace foo {
namespace bar {
baz();
}
}
with something like this:
namespace foo::bar {
baz();
}
?
Qualified names, like
something::someting_else
in C++ can only be used to refer to entities that have already been declared before. You cannot use such names to introduce something previously unknown. Even if the nested namespace was already declared before, extending that namespace is also considered as "introducing something new", so the qualified name is not allowed.You can use such names for defining functions previously declared in the namespace
but not declaring new namespaces of extending existing ones.
No, it's not. Instead of a bunch of indented nested namespaces, it's certainly valid to put them on the same line:
Update:
You can now nest namespaces more cleanly in C++17:
As per the grammar in $2.10, an identifier cannot have the token
":"
. So the namefoo::bar
is ill-formed.For anyone wondering, the form
namespace foo::bar
is supported since C++17. References:No; it's a syntax error.
You can combine namespaces into one name and use the new name (i.e. Foobar).