c++ - Why does the compiler stops the name lookup on overloads? -
c++ - Why does the compiler stops the name lookup on overloads? -
i read article:fun c++ namespaces author shows compiler stops looking overloads when encountering first one, here using namespaces.
namespace { void f(int x); // our std::sqrt(double) } namespace b { struct s {}; // user-defined type associated namespace b void f(s); void f(int, int); void test1() { using namespace a; // using directive f(1); // error namespace not considered because // b contains 2 overloads 'f' f(1,2); // ok b::f(int,int) f(b::s()); // ok b::f(s) } void test2() { using a::f; // using declaration f(1); // ok a::f(int) f(1,2); // error a::f hides b::f(int,int) f(b::s()); // ok b::f(s) due adl! } } namespace c { void test3() { using namespace a; // using directive f(1); // ok a::f(int) f(b::s()); // ok b::f(s) due adl! } void test4() { using a::f; // using declaration f(1); // ok a::f(int) f(b::s()); // ok b::f(s) due adl! } }
why compiler supposed stop?
edit #1: question indeed ment be: why standard says so?
thanks answers!
the compiler stops looking overloads when encountering first one
no, doesn't stop "when encountering first one" otherwise couldn't find both b::f(int,int)
, b::f(s)
.
it finds overloads in given scope (not first one), doesn't farther in more distant scopes.
that's name lookup in c++, if have global variable called var
, in function have local variable called var
, using name within function refer local variable. it's more useful way, it's more meant utilize variable in declared nearby, it's in related code.
if hands letter , tells give fred, standing few metres away wearing badge says "i fred", ignore him , go outside , maintain looking every other person in world called fred?
c++ compiler-construction
Comments
Post a Comment