std::remove_if and std::isspace - compile-time error

There is another overload of std::isspace, so you need to specify which one to use. An easy way is to use a lambda (or write your own one-line function if you don't have C++11 support):

std::remove_if(str.begin(), str.end(), 
               [](char c){ 
                  return std::isspace(static_cast<unsigned char>(c));
               });

std::isspace is an overloaded function, although the two overloads reside in different headers. Also note that your code may introduce undefined behaviour because only values in the range 0..UCHAR_MAX can be passed to std::isspace, whereas a char is possibly signed.

Here is a solution:

std::string str;
auto f = [](unsigned char const c) { return std::isspace(c); };
str.erase(std::remove_if(str.begin(), str.end(), f), str.end());

Tags:

C++

Gcc

G++