当使用“using namespace std”时,std::isgraph 不明确

2024-01-29

我尝试使用std::isgraph from <cctype>作为谓词find_if。但编译器出错说:

错误:没有匹配的函数可用于调用“find_if(__gnu_cxx::__normal_iterator >, __gnu_cxx::__normal_iterator >, 未解决的超载函数类型>)’

我用过using namespace std;据我了解,会有两个isgraph在全局命名空间中可见的函数。所以::isgraph或者简单地isgraph应该是不明确的并且std::isgraph不应含糊不清。相反,使用::isgraph可以,同时std::isgraph不是。

有人可以解释我错过了什么吗?一些相关的问题是用作 库中 find_if 中谓词的函数要求是什么? https://stackoverflow.com/questions/27971249/what-are-the-function-requirements-to-use-as-the-predicate-in-the-find-if-from-t and C++ 使用带有字符串的标准算法、带有 isdigit 的 count_if、函数强制转换 https://stackoverflow.com/questions/15039858/c-using-standard-algorithms-with-strings-count-if-with-isdigit-function-cast。但他们没有回答为什么明确指定std::仍然没有解析到该函数std命名空间。

EDIT:

#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编译了上面的代码


std::isgraph超载。

为了解决您的歧义could将其转换为相关的函数指针类型。

但为了正确使用它,参数应该转换为unsigned char,所以最好定义一个包装函数:

using Byte = unsigned char;

auto is_graphical( char const ch )
    -> bool
{ return !!isgraph( Byte( ch ) ); }

请注意,这仅适用于单字节编码,并且它取决于 C 级别的当前语言环境(请参阅setlocale).

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当使用“using namespace std”时,std::isgraph 不明确 的相关文章

随机推荐