I saw this example in cppreference's documentation for std::numeric_limits
#include <limits>
#include <iostream>
int main()
{
std::cout << "type\tlowest()\tmin()\t\tmax()\n\n";
std::cout << "uchar\t"
<< +std::numeric_limits<unsigned char>::lowest() << '\t' << '\t'
<< +std::numeric_limits<unsigned char>::min() << '\t' << '\t'
<< +std::numeric_limits<unsigned char>::max() << '\n';
std::cout << "int\t"
<< std::numeric_limits<int>::lowest() << '\t'
<< std::numeric_limits<int>::min() << '\t'
<< std::numeric_limits<int>::max() << '\n';
std::cout << "float\t"
<< std::numeric_limits<float>::lowest() << '\t'
<< std::numeric_limits<float>::min() << '\t'
<< std::numeric_limits<float>::max() << '\n';
std::cout << "double\t"
<< std::numeric_limits<double>::lowest() << '\t'
<< std::numeric_limits<double>::min() << '\t'
<< std::numeric_limits<double>::max() << '\n';
}
I don't understand the "+" operator in
<< +std::numeric_limits<unsigned char>::lowest()
I have tested it, replaced it with "-", and that also worked. What is the use of such a "+" operator?
These are the type of operators that act upon just a single operand for producing a new value. All the unary operators have equal precedence, and their associativity is from right to left. When we combine the unary operator with an operand, we get the unary expression.
Unary operators are operators which are used to calculate the result on only one operand. Unary operators are used on a single operand in order to calculate the new value of that variable.
Unary operators in C/C++ Unary operators: are operators that act upon a single operand to produce a new value. Types of unary operators: unary minus(-)
Unary Positive – Worthless Even if the value in money was negative 77 the value of the expression would be negative 77. The operator does nothing because multiplying anything by 1 does not change its value.
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())
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With