What is the purpose of a unary "+" before a call to std::numeric_limits<unsigned char> members?
The output operator <<
when being passed a char
(signed or unsigned) will write it as a character.
Those function will return values of type unsigned char
. And as noted above that will print the characters those values represent in the current encoding, not their integer values.
The +
operator converts the unsigned char
returned by those functions to an int
through integer promotion. Which means the integer values will be printed instead.
An expression like +std::numeric_limits<unsigned char>::lowest()
is essentially equal to static_cast<int>(std::numeric_limits<unsigned char>::lowest())
.
+
is there to turn the unsigned char
into an int
. The +
operator is value preserving, but it has the effect of inducing integral promotion on its operand. It's to make sure you see a numerical value instead of some (semi-)random character that operator <<
would print when given a character type.
Just to add a reference to the answers already given. From the CPP standard working draft N4713:
8.5.2.1 Unary operators
...
- The operand of the unary + operator shall have arithmetic, unscoped enumeration, or pointer type and the result is the value of the argument. Integral promotion is performed on integral or enumeration operands. The type of the result is the type of the promoted operand.
And char
, short
, int
, and long
are integral types.
Without +
the result will be different. The following snippet outputs a 97
instead of a a
char ch = 'a';
std::cout << ch << ' ' << +ch << '\n';
The reason is because different overloads prints different types of data.
There's no basic_ostream& operator<<( char value );
overload for std::basic_ostream
and it's explained at the end of the page
Character and character string arguments (e.g., of type
char
orconst char*
) are handled by the non-member overloads ofoperator<<
. Attempting to output a character using the member function call syntax (e.g.,std::cout.operator<<('c');
) will call one of overloads (2-4) and output the numerical value. Attempting to output a character string using the member function call syntax will call overload (7) and print the pointer value instead.
The non-member overload that will be called when you pass a char
variable is
template< class CharT, class Traits> basic_ostream<CharT,Traits>& operator<<(
basic_ostream<CharT,Traits>& os, char ch );
which prints out the character at the codepoint ch
So basically if you pass char
, signed char
or unsigned char
directly to the stream it'll print the character out. If you try removing the +
on the above lines you'll see that it prints some "strange" or non-visible characters which is not what one would expect
If you want their numerical values instead you must call the overload for short
, int
, long
or long long
. The easiest way to do this is promoting from char
to int
with unary plus +
. That's one of the rare useful applications of the unary plus operator. An explicit cast to int
will also work
There are many people who faced that problem on SO like
- cout not printing unsigned char
- std::cout deal with uint8_t as a character
- uint8_t can't be printed with cout