Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of a unary "+" before a call to std::numeric_limits<unsigned char> members?

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?

like image 567
Zhang Avatar asked Oct 18 '22 16:10

Zhang


People also ask

What is the purpose of unary operator in C?

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.

What does unary mean in C++?

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.

Why ++ is a unary operator?

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(-)

Why is unary positive worthless?

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.


1 Answers

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()).

like image 138
Some programmer dude Avatar answered Oct 20 '22 05:10

Some programmer dude