Following code results in an error as it is not considering ::x
in global scope.
#include<iostream>
namespace nspace
{
int x = 2;
}
int main()
{
using namespace nspace;
std::cout << ::x; //error: ‘::x’ has not been declared
return 0;
}
Following code results in output 2 without any compilation error.
#include<iostream>
namespace nspace
{
int x = 2;
}
using namespace nspace;
int main()
{
std::cout << ::x; // Outputs 2
return 0;
}
I was under the impression that if we have using directive within main function vs using directive in global scope, it is same as far as main function is concerned. For main function both should introduce nspace::x
in global scope. And both should result in same behaviour. But above code contradicts my understanding.
So if you can point me to some text from standard that clarifies above behaviour then it would be helpful.