C4204:使用了非标准扩展:非常量聚合初始值设定项

C4204: nonstandard extension used: non-constant aggregate initializer

我收到警告 C4204:使用了非标准扩展:非常量聚合初始值设定项 用于以下函数中的行 union {float f; uint_fast32_t i;} u = {x};

static inline int  __ef (float x)
{
union {float f; uint_fast32_t i;} u = {x};
#if defined( _C_ ) || defined( _C1_ )
        return (u.i >> 4) & 0xff;
#else
        return (u.i >> 3) & 0xff;
#endif
}

我该如何缓解该警告。

https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4204?view=vs-2019

With Microsoft extensions (/Ze), you can initialize aggregate types (arrays, structures, unions, and classes) with values that are not constants.

...

Such initializations are invalid under ANSI compatibility (/Za).

您可以使用 #pragma warning(disable:4204)

禁用警告