Search code examples
c++argument-dependent-lookup

ADL of class itself


According to standard argument dependent lookup adds to search set class if we have class type as function argument:

If T is a class type (including unions), its associated classes are: the class itself; the class of which it is a member, if any; and its direct and indirect base classes.

If so why foo cannot be found in this context:

class X{
public:
void foo(const X& ref){std::cout<<"Inner class method\n";}
};

int main(){
X x;
foo(x);
}

Shouldn't adp add to search set X class and look for foo in there?


Solution

  • No, because foo is a member function, not a free function which can be found through ADL.

    Perhaps you means this:

    static void foo(const X& ref){std::cout<<"Inner class method\n";}
    

    This also would not be found through ADL; you would need to qualify the call like X::foo(b).

    The clauses about associated classes are for friend functions declared in a class. For example:

    class X{
    public:
        friend void foo(const X& ref){std::cout<<"Inner class method\n";}
    };
    

    foo is a non-member function, but it can only be found through ADL.