7

I have

using namespace std;
vector<char> tmp;
tmp.push_back(val);
...

Now when I try

transform(tmp.begin(), tmp.end(), tmp.begin(), std::tolower);

It fails to compile, but this compiles:

transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);

What's the problem with std::tolower? It works with one argument, e.g., std::tolower(56) compiles. Thanks!

hovnatan
  • 1,331
  • 10
  • 23
  • 1
    Related/duplicate: http://stackoverflow.com/q/5270780/ (see the second answer http://stackoverflow.com/a/5270970/ ) – dyp Jul 23 '15 at 14:24
  • @dyp So if `std::lower` needs two arguments, how come `std::tolower(56)` works? – hovnatan Jul 23 '15 at 14:32
  • 1
    `std::tolower` is overloaded, there are two functions (one [function from the C library](http://en.cppreference.com/w/cpp/string/byte/tolower) taking one argument and [one function template](http://en.cppreference.com/w/cpp/locale/tolower) which takes two arguments) with the name `tolower` in namespace `std`. In the global namespace, if there is anything, then it's only the C library's `tolower` which takes one argument. – dyp Jul 23 '15 at 14:34

1 Answers1

3

std::tolower has two overloads and it cannot be resolved for the UnaryOperation where the C version ::tolower does not.

If you want to use the std::tolower you can use a lambda as

transform(tmp.begin(), tmp.end(), tmp.begin(), [](unsigned char c) {return std::tolower(c); });
NathanOliver
  • 171,901
  • 28
  • 288
  • 402