c++ - User-declared namespace member -
there 3.4.1/14:
if variable member of namespace defined outside of scope of namespace name appears in definition of member (after declarator-id) looked if definition of member occurred in namespace.
if name treated definition of member name point of declaration?
and why following example works:
namespace n { extern int j; } int = 2; int n::j = i; //n::j=2 int n::j=i actual appears namespace scope. hence declarationint i=2 not visible unqualified name lookup. why declaration found?
your question:
int n::j=iactual appears namespace scope. hence declarationint i=2not visible unqualified name lookup. why declaration found?
answer:
since
inot found innnamespace, looked in global namespace. hadibeen there innnamespace, have been used initializen::j.
hope following program clarifies doubt.
#include <iostream> namespace n { extern int j; extern int k; int x = 3; } int x = 2; int y = 10; int n::j = x; // n::x used initialize n::j int n::k = y; // ::y used initialize n::k int main() { std::cout << n::j << std::endl; std::cout << n::k << std::endl; } output:
3 10
update, in response comment op
what standard saying that:
namespace n { extern int j; } int x = 2; int n::j = x; is equivalent to:
namespace n { extern int j; } int x = 2; namespace n { int j = x; } the logic lookup ofx same. if found within same namespace n, used. if x not found in namespace n, searched outward in enclosing namespaces.
Comments
Post a Comment