Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C++ allow any integer literal to be implicitly converted to a short int?

int main()
{
    short n1 = 8ll; // no warning

    // warning C4305: 'initializing': truncation from '__int64' to 'short'
    // warning C4309: 'initializing': truncation of constant value
    short n2 = 88888ll;
}

My compiler is Visual Studio 2017.

According to cppref:

The type of the integer literal is the first type in which the value can fit, from the list of types which depends on which numeric base and which integer-suffix was used.

The integer literal with suffix ll should be of long long int; so short n1 = 8ll should trigger a warning like short n2 = 88888ll does.

Does C++ allow any integer literal to be implicitly converted to a short int if it is small enough?

like image 489
xmllmx Avatar asked May 04 '17 09:05

xmllmx


People also ask

What is implicit conversion in C?

Overview. Implicit type conversion in C language is the conversion of one data type into another datatype by the compiler during the execution of the program. It is also called automatic type conversion.

Can we pass int to short?

The C# specification allows an implicit conversion from int to short for constants, but not for other expressions. This is a reasonable rule, since for constants the compiler can ensure that the value fits into the target type, but it can't for normal expressions.

Can a short be assigned to an int without a cast?

Assigning a short value to an int without a cast is allowed because all of the values that can be represented by a short can also be represented by int. However, assigning an int value to a short is not allowed without a cast because it involves going from a 32-bit signed quantity to a 16-bit signed quantity.

How many types of conversion are there in C?

This type of conversion is known as implicit type conversion. In C, there are two types of type conversion: Implicit Conversion. Explicit Conversion.


1 Answers

The standard allows the implicit conversion between any two integer types, regardless of their values.

The compiler warnings are unrelated to the code being legal; the compiler just warns you when your code probably does not do what you wanted it to.

In your specific case, n1 would be 8 and n2 would have an implementation defined value. Both assignments are legal C++, but the latter is probably not what you intended.


Relevant standardese:

A prvalue of an integer type can be converted to a prvalue of another integer type. A prvalue of an unscoped enumeration type can be converted to a prvalue of an integer type.
If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2n where n is the number of bits used to represent the unsigned type). [ Note: In a two’s complement representation, this conversion is conceptual and there is no change in the bit pattern (if there is no truncation). — end note ]
If the destination type is signed, the value is unchanged if it can be represented in the destination type (and bit-field width); otherwise, the value is implementation-defined.

4.7/1-3 in N4141

like image 73
Baum mit Augen Avatar answered Nov 07 '22 05:11

Baum mit Augen