A while ago, someone with high reputation here on Stack Overflow wrote in a comment that it is necessary to cast a char-argument to unsigned char b
In C, toupper (and many other functions) take ints even though you'd expect them to take chars. Additionally, char is signed on some platforms and unsigned on others.
The advice to cast to unsigned char before calling toupper is correct for C. I don't think it's needed in C++, provided you pass it an I can't find anything specific to whether it's needed in C++.int that's in range.
If you want to sidestep the issue, use the toupper defined in std::locale. If you don't have any idea which locale to choose, use std::locale(""), which is supposed to be the user's preferred locale:
#include
#include
#include
#include
#include
int main()
{
std::string name("Bjarne Stroustrup");
std::string uppercase;
std::locale loc("");
std::transform(name.begin(), name.end(), std::back_inserter(uppercase),
[&loc](char c) { return std::toupper(c, loc); });
std::cout << name << '\n' << uppercase << '\n';
return 0;
}