Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 432
Pranit Kothari Avatar asked Dec 05 '25 08:12

Pranit Kothari


1 Answers

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

like image 181
songyuanyao Avatar answered Dec 07 '25 21:12

songyuanyao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!