Consider the following two statements:
namespace foo = bar;
and
namespace foo {
using namespace bar;
}
Are those two statements equivalent, or are there some subtle differences I'm not aware of?
(Please note that this is not a question about coding style - I'm just interested in C++ parsing).
Namespace declarations can be nested within another namespace. Namespace declarations don't have access specifiers (Public or Private). No need to give a semicolon after the closing brace of the definition of namespace.
namespace alias definition Namespace aliases allow the programmer to define an alternate name for a namespace. They are commonly used as a convenient shortcut for long or deeply-nested namespaces.
You can also create an alias for a namespace or a type with a using alias directive.
Namespaces provide a method for preventing name conflicts in large projects. Symbols declared inside a namespace block are placed in a named scope that prevents them from being mistaken for identically-named symbols in other scopes. Multiple namespace blocks with the same name are allowed.
namespace foo=bar;
This does not affect any name lookup rules. The only affect is to make 'foo' an alias to 'bar'. for example:
namespace bar
{
void b();
}
void f () {
bar::b (); // Call 'b' in bar
foo::b (); // 'foo' is an alias to 'bar' so calls same function
}
The following does change lookup rules
namespace NS
{
namespace bar
{
}
namespace foo {
using namespace bar;
void f () {
++i;
}
}
}
When lookup takes place for 'i', 'foo' will be searched first, then 'NS' then 'bar'.
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