当我使用auto bi = 123456789时,在C++中,它总是被指定为int吗?

jor*_*iva 2 c++ gcc

如果我想要bi是一个long int,不可能使用auto,因为它总是指定为int?

wal*_*lly 11

一些选择:

auto bi = "123456789";          // const char*
auto bi2 = 12345;               // int
auto bi3 = 123456789;           // int (when int is 32 bits or more )
auto bi4a = 123456789L;         // long
auto bi4b = 178923456789L;      // long long! (L suffix asked for long, but got long long so that the number can fit)
auto bi5a = 123456789LL;        // long long
auto bi5b = 123456784732899;    // long long (on my system it is long long, but might be different on ILP64; there is would just be an int)
auto bi6 = 123456789UL;         // unsigned long
auto bi7 = 123456789ULL;        // unsigned long long
Run Code Online (Sandbox Code Playgroud)

以上所有示例都取决于您使用的系统.

在标准中,在[lex.icon] 表5中 -引用了整数文字的类型:

整数文字的类型是表5中相应列表中的第一个,其中可以表示其值.

如果我们看一下表为十进制文字,我们看到的,甚至影响UL后缀取决于可容纳多大尺寸:

在此输入图像描述

  • 请注意,在ILP64架构上,`bi5b`将只是`int`(即`long long`将是不正确的); 见(http://www.unix.org/whitepapers/64bit.html). (3认同)