C++ 是否允许将任何整数文字隐式转换为 short int?
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;
}
我的编译器是 Visual Studio 2017.
根据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.
后缀为ll
的整数字面量应为long long int
;所以 short n1 = 8ll
应该像 short n2 = 88888ll
一样触发警告。
C++ 是否允许将任何整数文字隐式转换为 short int
(如果它足够小)?
标准允许任意两个整数类型之间的隐式转换,无论它们的值如何。
编译器警告与代码是否合法无关;编译器只是在您的代码可能未按照您的要求执行时发出警告。
在您的特定情况下,n1
将是 8,而 n2
将具有实现定义的值。这两个赋值都是合法的 C++,但后者可能不是您想要的。
相关标准语:
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.
N4141 中的 4.7/1-3
是的,整数可以隐式转换。这些是 C++ 标准草案 N4296 中的规则:
4.7 Integral conversions
1 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.
2 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 ]
3 If the destination type is signed, the value is unchanged if it can
be represented in the destination type; otherwise, the value is
implementation-defined.
4 If the destination type is bool, see 4.12.
If the source type is bool, the value false is converted to zero and
the value true is converted to one.
5 The conversions allowed as
integral promotions are excluded from the set of integral conversions.
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;
}
我的编译器是 Visual Studio 2017.
根据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.
后缀为ll
的整数字面量应为long long int
;所以 short n1 = 8ll
应该像 short n2 = 88888ll
一样触发警告。
C++ 是否允许将任何整数文字隐式转换为 short int
(如果它足够小)?
标准允许任意两个整数类型之间的隐式转换,无论它们的值如何。
编译器警告与代码是否合法无关;编译器只是在您的代码可能未按照您的要求执行时发出警告。
在您的特定情况下,n1
将是 8,而 n2
将具有实现定义的值。这两个赋值都是合法的 C++,但后者可能不是您想要的。
相关标准语:
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.
N4141 中的 4.7/1-3
是的,整数可以隐式转换。这些是 C++ 标准草案 N4296 中的规则:
4.7 Integral conversions
1 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.
2 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 ]
3 If the destination type is signed, the value is unchanged if it can be represented in the destination type; otherwise, the value is implementation-defined.
4 If the destination type is bool, see 4.12. If the source type is bool, the value false is converted to zero and the value true is converted to one.
5 The conversions allowed as integral promotions are excluded from the set of integral conversions.