【发布时间】:2016-02-27 17:49:57
【问题描述】:
我尝试使用来自<cctype> 的std::isgraph 作为find_if 中的谓词。但是编译器错误地说:
错误:没有匹配函数调用 'find_if(__gnu_cxx::__normal_iterator >, __gnu_cxx::__normal_iterator >, 未解决的重载函数类型>)'
我使用了using namespace std;,据我了解,在全局命名空间中将有两个isgraph 函数可见。所以::isgraph 或简单的isgraph 应该是模棱两可的,std::isgraph 不应该是模棱两可的。相反,使用::isgraph 可以,而std::isgraph 则不行。
谁能解释我错过了什么?一些相关问题是What are the function requirements to use as the predicate in the find_if from the <algorithm> library? 和C++ using standard algorithms with strings, count_if with isdigit, function cast。但他们没有回答为什么明确指定 std:: 仍然无法解析到 std 命名空间中的函数。
编辑:
#include <cctype>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;
int main()
{
string root_line = "hello";
auto ind = distance(root_line.begin(), find_if(root_line.begin(), root_line.end(), std::isgraph));
cout << ind;
return 0;
}
我用4.8.4版本的g++ -std=c++11编译了上面的代码
【问题讨论】:
-
你能举几个我可以编译和玩的例子吗?
-
它对我有用,所以这很奇怪。
-
@Rich 这在 VS2015 中完全有效,但似乎在其他任何地方都没有。
-
如果你去掉 using 并把
std::放在前面,错误仍然存在,所以这是一个红鲱鱼。 -
@NeilKirk 哇,谢谢! cppreference 自己的搜索未在
标头中显示该搜索。这解决了这个谜。
标签: c++