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

瘦欲@ 提交于 2019-12-08 14:37:59

问题


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


回答1:


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




回答2:


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




回答3:


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.




回答4:


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.




回答5:


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.




回答6:


Take a look at http://en.wikibooks.org/wiki/C++_Programming/Scope/Namespaces




回答7:


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.



来源:https://stackoverflow.com/questions/1495649/what-is-the-scope-of-a-namespace-alias-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!