Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does 'auto' not respect the unary minus operator?

I'm quite new to C++ but I find this behaviour of auto weird:

class A{};  int main() {     A a;     auto x = -(sizeof(a));     cout << x << endl;     return 0; } 

Variable x is unsigned in this case although I used the unary minus operator at the initialiation of the variable. How come that only the return type of sizeof (std::size_t) is considered but not the fact that the stored number will be negative because of the used operator?

I'm aware of size_t being an unsigned int.

I've tried this with GCC 8.1.0 and C++17.

like image 845
CodeShark Avatar asked Jul 25 '18 08:07

CodeShark


People also ask

What is a unary minus operator?

The - (unary minus) operator negates the value of the operand. The operand can have any arithmetic type. The result is not an lvalue. For example, if quality has the value 100 , -quality has the value -100 . The result has the same type as the operand after integral promotion.

How do you use unary minus?

In some contexts, - is the unary minus operator. In other contexts, - is the subtraction operator. asks for 12 to be subtracted from 95. means add 3 to negative 12 (resulting in -9).

Why ++ is a unary operator?

Unary Increment (++) In this type, the Unary Operator is denoted by the '++' sign. It increases the value by 1 to the operand and then stores the value to the variable. It works for both Prefix and Postfix positions.

What is the unary minus operator?

Unary Minus If you look in the table of operatorsyou will see that the character -is listed twice. That is because -is used for two purposes. In some contexts, -is the unary minusoperator. In other contexts, -is the subtractionoperator. The unary minus is used to show a negative number. For example: -97.34

What is the use of unary NOT (!) IN C?

NOT (!) The minus operator changes the sign of its argument. A positive number becomes negative, and a negative number becomes positive. unary minus is different from subtraction operator, as subtraction requires two operands. It is used to increment the value of the variable by 1. The increment can be done in two ways:

What is the difference between unary minus and subtraction?

unary minus is different from subtraction operator, as subtraction requires two operands. It is used to increment the value of the variable by 1. The increment can be done in two ways: In this method, the operator precedes the operand (e.g., ++a).

How do you use unary minus in C?

A positive number becomes negative, and a negative number becomes positive. unary minus is different from subtraction operator, as subtraction requires two operands. It is used to increment the value of the variable by 1. The increment can be done in two ways: In this method, the operator precedes the operand (e.g., ++a).


Video Answer


1 Answers

The actual issue here is that use of unary minus operator, just like the rest of built-in arithmetic operators, is a subject to integral promotions. So surprisingly the result of applying unary minus to size_t will be still size_t and there is no need to blame auto.

Counter-example. In this case due to integral promotions type of x will be int so output will be -1:

unsigned short a{1}; auto x{-a}; cout << x << endl; 
like image 172
user7860670 Avatar answered Sep 17 '22 06:09

user7860670