Search code examples
c++visual-c++argument-dependent-lookup

Why function fun is not qualified for ADL?


I have simple namespace, which has one variable and one function. In main I try to call function without namespace qualifier, and variable with namespace qualifier.

namespace SAM
{
    int p = 10;
    void fun(int)
    {
        cout<<"Fun gets called";
    }
} 

int main()
{

    fun(SAM::p);//why SAM::fun is not get called?
    return 0;
}

I am not able to call fun, why it is not qualified for ADL (Argument-dependant name lookup)?

I am getting following error in Visual Studio.

'fun': identifier not found

If I use SAM::fun, it works.


Solution

  • ADL is adopted for type, not variable, e.g.

    namespace SAM
    {
        struct P {};
        void fun(P)
        {
            cout<<"Fun gets called";
        }
    } 
    
    int main()
    {
        SAM::P p;
        fun(p);
        return 0;
    }
    

    In the C++ programming language, argument-dependent lookup (ADL), or argument-dependent name lookup, applies to the lookup of an unqualified function name depending on the types of the arguments given to the function call.

    Reference: Argument-dependent name lookup