为什么给Predicate参数时不能传"std"前缀
Why can not pass "std" prefix when giving Predicate parameter
我有这个代码:
#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 传递时,比如 std::isxdigit
,我得到这个错误:
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;
}
我有这个代码:
#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 传递时,比如 std::isxdigit
,我得到这个错误:
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;
}