Visual Studio 2015 更新 3.C++11\C++14。
对于 int
:
constexpr int ci1 {50};
constexpr int ci2 {500};
char c1 {ci1}; // OK
char c2 {ci2}; // compilation error
即通用统一初始化可以在编译模式下检查值(从int
到char
)。 Bjarne Stroustrup 在他的书中提到了这个案例。
我预计这对于 double
到 int
的情况是正确的,但它不适用于 Visual Studio 2015 Update 3:
constexpr double cd {4.0};
int i1 {cd}; // compilation error
错误信息:
C2397 conversion from 'double' to 'int' requires a narrowing conversion
这是正确的行为(对于我的第二个代码示例)还是特定于 Visual Studio?
最佳答案
double
至 int
转换总是被认为是缩小的,这就是 Bjarne Stroustrup 创建 narrow_cast<>
的原因在他的书中实现了这种转换。
关于c++ - 通用统一初始化 : `double` to `int` ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38676274/