1

I have found on the cpp reference website (link) the following code which I do not understend completely:

void try_widen(const std::ctype<wchar_t>& f, char c)
{
    wchar_t w = f.widen(c);
    std::cout << "The single-byte character " << +(unsigned char)c
              << " widens to " << +w << '\n';
}

What I do not understand is what does plus sign "+" makes before w variable (+w) and before (unsigned char)c (+(unsigned char)c). Can somebody explain me this?

joe_specimen
  • 295
  • 3
  • 19

1 Answers1

2

Like all arithmetic operations, unary plus triggers integer promotion of its operand. This results in printing a numerical value of a character. Without it, a different overload of operator << could be chosen. << (unsigned char)c would print a character rather than a number. I believe std::cout << w would not compile.

Eugene
  • 6,194
  • 1
  • 20
  • 31