c++ - 提供Predicate参数时为什么不能传递 “std”前缀

标签 c++ c++11 std

我有以下代码:

#include <cctype>
#include <algorithm>

int main()
{
    std::string str("ABCD");
    std::all_of(str.begin(), str.end(), ::isxdigit);
    return 0;
}
std::all_of需要谓词作为最后一个参数。我要传递的是std::isxdigit。当我将其作为::isxdigit传递时,可以正常工作,但是当我将其与std::isxdigit一起传递给 std 时,我得到了错误:
12:58: error: no matching function for call to 'all_of(std::basic_string<char>::iterator, std::basic_string<char>::iterator, <unresolved overloaded function type>)'
12:58: note: candidate is:
In file included from /usr/include/c++/4.9/algorithm:62:0,
                 from 6:
/usr/include/c++/4.9/bits/stl_algo.h:508:5: note: template<class _IIter, class _Predicate> bool std::all_of(_IIter, _IIter, _Predicate)
     all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
     ^
/usr/include/c++/4.9/bits/stl_algo.h:508:5: note:   template argument deduction/substitution failed:
12:58: note:   couldn't deduce template parameter '_Predicate'

为什么会出现此错误?
如果它确实是std,用 std 前缀传递它有什么问题?

最佳答案

为了使其与std::isxdigit一起使用,您应该编写类似以下内容的代码:

#include <cctype>
#include <string>
#include <algorithm>

int main()
{
    std::string str("ABCD");
    std::all_of(str.begin(), str.end(), [](unsigned char c){ return std::isxdigit(c); });
    return 0;
}

Demo

关于c++ - 提供Predicate参数时为什么不能传递 “std”前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60276348/

相关文章:

c++ - 超过 64 个线程时 EnterCriticalSection 崩溃

c++ - wchar_t* 到 short int 的转换

c++ - std::string.substr 运行时错误

c++ - 你如何对每个元素执行转换并将结果附加到 C++ 中?

c++ - 如何开发调试器

c++ - 异常安全和 make_unique

c++ - 在函数调用中,为什么 nullptr 不匹配指向模板对象的指针?

c++ - std::function/std::bind 的生命周期管理(在 Windows PostMessage 中传递一个仿函数作为 lparam)

c++ - 如何在 Visual Studio 2010 中使用 tr1 (tr1::function)?

C++:调用父类的虚函数