不一致的警告 "conversion from 'const unsigned char' to 'const float' requires a narrowing conversion"

Inconsistent warning "conversion from 'const unsigned char' to 'const float' requires a narrowing conversion"

Visual C++ 2017 和 gcc 5.4 为 Line B 生成 conversion from 'const unsigned char' to 'const float' requires a narrowing conversion 警告,但在此代码段中不为 Line A 生成警告:

#include <iostream>

int main() {
    const unsigned char p = 13;
    const float         q = p;  // Line A

    std::cout << q << '\n';

    const unsigned char c[3] = {0, 1, 255};
    const float         f[3] = {c[2], c[0], c[1]};  // Line B

    for (auto x:f)
        std::cout << x << '\n';
}

这个警告有效吗?为什么 Line B 的处理方式与 Line A 不同?

警告有效,来自 C++11 narrowing conversions are prohibited in aggregate initialization; but not applied in copy initialization(和以前一样)。

If the initializer clause is an expression, implicit conversions are allowed as per copy-initialization, except if they are narrowing (as in list-initialization) (since C++11).

Until C++11, narrowing conversions were permitted in aggregate initialization, but they are no longer allowed.

list-initialization limits the allowed implicit conversions by prohibiting the following:

  • conversion from an integer type to a floating-point type, except where the source is a constant expression whose value can be stored exactly in the target type

顺便说一句:c[0]c[1]c[2] not constant expressions; you might declare the array as constexpr, i.e. constexpr unsigned char c[3] = {0, 1, 255};. Then exception applied and Line B 也可以正常工作。