EmpireJones
EmpireJones

Reputation: 3086

What is the scope of a namespace alias?

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

Upvotes: 22

Views: 5511

Answers (7)

rjoshi
rjoshi

Reputation: 1673

It's a block duration of validity.

For example: If you define a namespace alias as below, the namespace alias abc would be invalid outside the {...}-block.

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

Upvotes: 21

Huitse Tai
Huitse Tai

Reputation: 73

It is valid for the duration of the scope in which it is introduced.

Take a look at http://en.cppreference.com/w/cpp/language/namespace_alias, I trust the explanation of cppreference, it's much more standard.

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564931

The scope is the declarative region in which the alias is defined.

Upvotes: 1

Joanne C
Joanne C

Reputation: 1115

As far as I know, it's in the scope it's declared. So, if you alias in a method, then it's valid in that method, but not in another.

Upvotes: 0

Twisol
Twisol

Reputation: 2772

I'm fairly certain that a namespace alias only has scope within the block it's created in, like most other sorts of identifiers. I can't check for sure at the moment, but this page doesn't seem to go against it.

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 755094

It would have the scope of the block in which it was defined - likely to be the same as function scope unless you declare the alias inside a block within a function.

Upvotes: 0

Related Questions