Reputation: 1261
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).
Upvotes: 10
Views: 9549
Reputation: 21721
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'.
Upvotes: 16
Reputation: 3074
As you are importing a namespace into another, then yes, it should be equal in that respect. However, the second one also allows for other code to be placed within, so you can also put things which are not part of namespace foo within it. The former merely creates an alias.
Upvotes: 2