#include template

Clang ++: error: the call to the "section" is ambiguous

#include <algorithm> #include <vector> template <class BidirectionalIterator, class UnaryPredicate> BidirectionalIterator partition(BidirectionalIterator first, BidirectionalIterator last, UnaryPredicate pred) { while (first != last) { while (pred(*first)) { ++first; if (first == last) return first; } do { --last; if (first == last) return first; } while (!pred(*last)); std::swap(*first, *last); ++first; } return first; } int main() { std::vector<int> v = { 1, 55, 17, 65, 40, 18, 77, 37, 77, 37 }; partition(v.begin(), v.end(), [](const int &i) { return i < 40; }); return 0; } 

The code will not compile. Both clang ++ (3.5.2 / cygwin) and Visual Studio (2013) complain about an ambiguous call. Since the using directive is not used, I do not understand what happened. For successful compilation, the :: prefix is ​​used.

+7
c ++
source share
1 answer

Your partition has a name clash with std::partition

The reason this is done, even without the std:: prefix, is because it uses argument-dependent search (ADL) for arguments that are std::vector<int>::iterator that carry the namespace std:: . Therefore, the compiler can “see” the std::partition function, as well as your partition function.

From cppreference (highlighted by me)

... for each argument in the function call expression and for each argument of the template function template, its type is checked to determine the associated set of namespaces and classes that it will add to the search

+8
source share

All Articles