Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the scope of a namespace alias in C++?

Tags:

Does a C++ namespace alias defined inside a function definition have a block, function, file, or other scope (duration of validity)?

like image 338
EmpireJones Avatar asked Sep 30 '09 00:09

EmpireJones


People also ask

What is the scope of a namespace?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.

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 the benefits of using namespace?

Advantages of namespace In one program, namespace can help define different scopes to provide scope to different identifiers declared within them. By using namespace - the same variable names may be reused in a different program.


1 Answers

It's a block duration of validity. E.g If you define a namespace alias as below, the namespace alias abc would be invalid outside {...} block.


 {  
    namespace abc = xyz;
    abc::test t;  //valid 
 }
  abc::test t;  //invalid

like image 69
rjoshi Avatar answered Oct 06 '22 05:10

rjoshi