[英]something about C++ unnamed namespace
#include <iostream>
namespace
{
int a=1;
}
int a=2,b=3;
int main(void)
{
std::cout<<::a<<::b;
return 0;
}
I complie it with my g++,but the output is 23, who can explain it? 我用我的g ++编译它,但输出是23,谁可以解释它? is that a way to get access to the <unnamed> namespace ::a
? 是一种访问<unnamed> namespace ::a
吗?
No, you can't. 不,你不能。 You can work around it thus: 你可以这样解决它:
namespace
{
namespace xxx
{
int a = 1;
}
}
...
std::cout << xxx::a << ::b;
::
in ::a
refers to the global namespace. ::
in ::a
指的是全局命名空间。 Anonymous namespace should be accessed via just a
(or to be more specific, you shouldn't do like this at all) 应该只通过a
访问匿名命名空间(或者更具体地说,你根本不应该这样做)
Using unnamed namespaces, this is not possible. 使用未命名的命名空间,这是不可能的。 Refer the below article 请参阅以下文章
http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/unnamed_namespaces.htm http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/unnamed_namespaces.htm
You have to go for named namespaces. 你必须去命名命名空间。
You can access the global namespace, but don't redefine it. 您可以访问全局命名空间,但不要重新定义它。
#include <iostream>
namespace
{
int a=1;
}
int b=3;
int main(void)
{
std::cout<<::a<<::b;
return 0;
}
here the out is 13. 这里出局是13。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.