Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is 'using namespace' inside another namespace equivalent to an alias?

Tags:

c++

namespaces

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

like image 476
pokita Avatar asked Aug 13 '09 10:08

pokita


People also ask

Can you define one namespace inside another namespace?

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.

What is a namespace alias?

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.

Can we create alias of a namespace?

You can also create an alias for a namespace or a type with a using alias directive.

What is a namespace why would you choose to use a namespace other than standard?

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.


1 Answers

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

like image 58
Richard Corden Avatar answered Oct 13 '22 20:10

Richard Corden