#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? is that a way to get access to the <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. Anonymous namespace should be accessed via just a
(or to be more specific, you shouldn't do like this at all)
Using unnamed namespaces, this is not possible. Refer the below article
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.