Почему нельзя передать префикс "std" при задании параметра Predicate - PullRequest
1 голос
/ 18 февраля 2020

У меня есть этот код:

#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?

1 Ответ

2 голосов
/ 18 февраля 2020

Чтобы заставить его работать с 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;
}

Демо

...