1

I have this code:

#include <cctype>
#include <algorithm>

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

std::all_of requires a Predicate as last argument. What I want to pass is std::isxdigit. When I pass it as ::isxdigit it works fine, but when I pass it with std, like std::isxdigit, I get this error:

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'

Why am I getting this error? What is wrong with passing it with std prefix, if it DO is of std?

Mert Mertce
  • 1,049
  • 7
  • 34
  • Does this answer your question? https://stackoverflow.com/questions/6658767/c-name-space-confusion-std-vs-vs-no-prefix-on-a-call-to-tolower – mfnx Feb 18 '20 at 08:17
  • [You can't take the address of a standard library function](https://stackoverflow.com/a/55687045/5376789). – xskxzr Feb 18 '20 at 08:28
  • Reopen: The given duplicate for closing (https://stackoverflow.com/q/6658767/1741542) is about `using namespace std`. This is not the case here. – Olaf Dietsche Mar 01 '20 at 10:27

1 Answers1

2

In order to make it working with std::isxdigit you should write something like:

#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

NutCracker
  • 11,485
  • 4
  • 44
  • 68
  • It should also make the code faster - as otherwise isxdigit will be treated as function pointer inside the template which can result in slow unoptimizable code. – ALX23z Feb 18 '20 at 08:20