Reputation: 5809
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
}
Upvotes: 0
Views: 241
Reputation: 1319
The following function invocation:
fun();
Contains no arguments, so there's no way it could trigger argument-dependent lookup.
To clarify, "arguments" refer to the actual values/references passed when a function is invoked, as opposed to "parameters" which are the names/types that a function specifies in its declaration.
Upvotes: 2
Reputation: 170065
When the compiler encounters a function call expression, it first builds an overload set of possible functions to call, and then performs overload resolution. ADL is part of the overload set generation.
Now, when coming to this statement:
fun();
First, it looks in the global namespace for possible overloads. And it finds none.
Then it does ADL based on the arguments passed, but you didn't pass any arguments.
The overload set is therefore empty.
Upvotes: 3
Reputation: 45654
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?
Upvotes: 2