I think you yourself posed the problem:
in the global namespace
Functions in the global namespace are considered last. This is the most external coverage by definition. Any function with the same name (not necessarily applicable) that is in a closer space (from the point of view of the call) will be raised first.
template <typename Rng, typename T> typename Rng::iterator find( Rng& rng, T const& t ); namespace foo { bool find(std::vector<int> const& v, int); void method() { std::deque<std::string> deque; auto it = find(deque, "bar"); } }
Here (if vector or deque does not include algorithm , which is allowed), the only method that will be selected during the name lookup will be:
bool foo::find(std::vector<int> const&, int);
If algorithm is somehow enabled, it will also:
template <typename FwdIt> FwdIt std::find(FwdIt begin, FwdIt end, typename std::iterator_traits<FwdIt>::value_type const& value);
And, of course, overload resolution will not claim that there is no match.
Note that searching by name is very dumb: neither argument nor argument type is considered!
Therefore, there are only two kinds of free functions that you should use in C ++:
- Those that are part of the interface of a class declared in the same namespace are caught by ADL
- Those that are not, and that you must explicitly qualify to avoid this type of problem
If you fall out of these rules, it may work or not, depending on what is included and what is very inconvenient.
Matthieu M.
source share