Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

c++ - Why does MSVC pick a long long as the type for -2147483648?

My snippet:

auto i = -2147483648;
int j = 3;
std::swap(i, j); // Compile error about mismatched types here. 

The compiler states that the literal i is a long long. Why is that? -2147483648 fits in an int on MSVC x64.

My compiler is MSVC, target is 64 bits.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Contrary to popular belief, -2147483648 is not a literal: C++ does not support negative literal values.

It is, in fact, a compile-time evaluable constant expression consisting of a unary negation of the literal 2147483648.

On MSVC x64, which has 32 bit ints and longs, 2147483648 is too big for either of those so it fails over to the long long type that you observe.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...