为什么MSVC会选择很长的时间作为-2147483648的类型? [英] Why does MSVC pick a long long as the type for -2147483648?

查看:51
本文介绍了为什么MSVC会选择很长的时间作为-2147483648的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码段:

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

编译器声明文字 i 长长。这是为什么? -2147483648适用于MSVC x64上的 int

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

我的编译器是MSVC,目标是64位。

My compiler is MSVC, target is 64 bits.

推荐答案

与流行的看法相反,-2147483648不是不是文字:C ++不支持负文字值。

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

实际上,它是一个编译时可评估的常量表达式,由 literal 的一元否定组成2147483648。

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

在MSVC x64上,它具有32位 int s和 long s,2147483648对于任何一个都太大了,因此它会故障转移到您观察到的 long long 类型。

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.

这篇关于为什么MSVC会选择很长的时间作为-2147483648的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆