【问题标题】:Namespaced std symbol interfiers with a global symbol [duplicate]命名空间标准符号干扰全局符号[重复]
【发布时间】:2019-08-01 16:28:58
【问题描述】:

使用这个全局命名空间 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 << "Found\n";
  return 0;
}

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

【问题讨论】:

  • 是的,同样的原因begin(v) 工作而不是std::begin(v)。检查副本。

标签: c++


【解决方案1】:

这是由于Argument-Dependent Lookup (ADL)

为避免通过 ADL,请写:

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

改为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    • 1970-01-01
    • 2016-07-21
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多