具有全局符号的命名为STD符号Interfier

Namespaced std symbol interfiers with a global symbol

本文关键字:符号 STD Interfier 命名为 全局      更新时间:2023-10-16

使用此全局名称空间find((模板函数:

#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 << "Foundn";
  return 0;
}

为什么,编译器(clang(失败,表明有两个候选模板函数:我的find((和标准std :: find((?

这是由于参数依赖性查找(adl(。

要避免通过ADL,请写下:

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

而不是。