2

With this global namespace find() template function:

#include <iostream>
#include <vector>

template <typename Iterator, typename T>
Iterator find(Iterator first, Iterator last, T value) {
  while(first!=last && *first!=value)
      ++first;
  return first;
}

int main(int argc, const char * argv[]) {
  std::vector<int> v = {1,3,5,7};
  std::vector<int>::iterator pos = find(begin(v), end(v), 3);
  if (pos != end(v))
    std::cout << "Found\n";
  return 0;
}

Why, the compiler (clang) fails indicating that there are two candidate template functions: my find() and the standard std::find()?

Freeman
  • 5,810
  • 3
  • 47
  • 48

1 Answers1

2

This is due to Argument-Dependent Lookup (ADL).

To avoid going through ADL, write:

std::vector<int>::iterator pos = ::find(begin(v), end(v), 3);

instead.

Acorn
  • 24,970
  • 5
  • 40
  • 69