For practice, I wrote some template functions whose names are the same as the stl algorithms. But my code can not compile
error: Call to < algorithm_name > is ambiguous.
I only included using std::necessary_names; in my code rather than using namespace std;.
Usually when you have
using, the “used” name takes precedence:However, Argument-Dependent Lookup can mess this up:
So, if you are having trouble, qualify your own namespace (in this example,
M) explicitly:In a somewhat bizarre twist, you can also use parentheses to prevent ADL:
Explanation
i.e. Normal lookup stops at the name that you brought into scope with
using, but when ADL comes into play, other names are also added to the candidate set, causing an ambiguity between two names.