Does a C++ namespace alias defined inside a function definition have a block, function, file, or other scope (duration of validity)?
Asked
Active
Viewed 5,289 times
22
-
Do you mean "using [namespace];" ? – Nick Bedford Sep 30 '09 at 00:56
-
It looks like he means the aliasing of a longer namespace name to that of a more easily used identifier: http://msdn.microsoft.com/en-us/library/chwe1tc8.aspx – Twisol Sep 30 '09 at 01:00
-
4namespace alias (and using declarations) have the same scoping rules as other declarations in C++. – Martin York Sep 30 '09 at 01:46
7 Answers
21
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

Anton Kesy
- 119
- 7

rjoshi
- 1,645
- 1
- 20
- 31
1
The scope is the declarative region in which the alias is defined.

Reed Copsey
- 554,122
- 78
- 1,158
- 1,373
0
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.

Jonathan Leffler
- 730,956
- 141
- 904
- 1,278
0
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.

Joanne C
- 1,115
- 6
- 10
0
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.

Huitse Tai
- 73
- 6
-
Please don't provide only a link. Also add at least a partial excerpt from the page you are referencing. – Matthias Oct 28 '13 at 02:13