Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicit conversion: is the following warning valid?

This question Implicit type conversion rules in C++ operators (and several others) state

If either is long long unsigned int the other is promoted to long long unsigned int

However if I do the following under MSVC:

unsigned int a = <some expression>;
unsigned long long b = a << 32ULL;

The second line generates the following warning:

warning C4293: '<<': shift count negative or too big, undefined behavior

32ULL is a 64 bit unsigned value, therefore according to the implicit conversion rules this should mean that a is converted to unsigned long long as well. Hence I'm shifting a 64 bit value by 32 bits, clearly a well defined operation.

Is MSVC bugged or is there a flaw in my logic?

like image 255
dgnuff Avatar asked Aug 15 '17 17:08

dgnuff


People also ask

Which is implicit conversion as following?

An implicit conversion sequence is the sequence of conversions required to convert an argument in a function call to the type of the corresponding parameter in a function declaration. The compiler tries to determine an implicit conversion sequence for each argument.

What is the example of implicit conversion?

Implicit conversions: No special syntax is required because the conversion always succeeds and no data will be lost. Examples include conversions from smaller to larger integral types, and conversions from derived classes to base classes.

Is implicit type conversion bad?

Implicit conversions allow the compiler to treat values of a type as values of another type. There's at least one set of scenarios in which this is unambiguously bad: non-total conversions. That is, converting an A to a B when there exists A s for which this conversion is impossible.

What is implicit type conversion in SQL?

Implicit conversions are not visible to the user. SQL Server automatically converts the data from one data type to another. For example, when a smallint is compared to an int, the smallint is implicitly converted to int before the comparison proceeds.


1 Answers

Shifts don't do the so-called "usual arithmetic conversions", which is the rules you cited. They only perform integral promotions. The result of a shift is of the same type as the promoted left operand.

like image 108
T.C. Avatar answered Oct 23 '22 11:10

T.C.