tags:

views:

310

answers:

2

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).

+2  A: 

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.

Electro
And if names are added into namespace 'foo' then the different lookup rules may result in different names being found between the two examples.
Richard Corden
+6  A: 
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'.

Richard Corden