Search code examples
c++argument-dependent-lookupdefault-arguments

Why doesn't argument dependent lookup work with default arguments?


I wrote the following code half expecting the default argument to trigger ADL. It doesn't (I got a compiler error instead).
Are explicit arguments required to trigger ADL?

#include <iostream>

namespace sheldon
{
    enum FLAG{ USA , UK , EU };

    void fun( FLAG f = USA )
    {
        std::cout << "Fun with flags!" << std::endl;
    }
}

int main()
{
    fun();  // Does not compile
    // fun( sheldon::USA ); // compiles
}

Solution

  • ADL only works with the arguments you give it, otherwise things would be really horrible, namespaces would become useless in isolating their contents.
    Think what would happen if you had this as well:

    namespace fun {
        struct tag {};
        void fun(tag = {})
        {
            std::cout << "Fun with tags!" << std::endl;
        }
    }
    

    Shall we have fun with flags or tags?