使用MSVC编译器下的最大成员初始化联盟 [英] Initialize union using largest member under MSVC compiler

查看:104
本文介绍了使用MSVC编译器下的最大成员初始化联盟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在C ++库(确切地说是C ++ 03)中将LARGE_INTEGER初始化为0.以前,初始化为:

I'm trying to initialize a LARGE_INTEGER to 0 in a C++ library (C++03 to be exact). Previously, the initialization was:

static LARGE_INTEGER freq = { 0 };

在MinGW之下,它发出了警告:

Under MinGW it produced a warning:

缺少成员'_LARGE_INTEGER :::: HighPart'的初始化程序

missing initializer for member '_LARGE_INTEGER::::HighPart'

因此,我根据可以在声明中初始化联合吗?:

So I changed the initialization to the following in accordance with Can a union be initialized in the declaration?:

static LARGE_INTEGER freq = { .QuadPart = 0 };

我现在正在Visual Studio 2015下进行测试,并且会产生错误:

I'm now testing under Visual Studio 2015, and its producing an error:

81  static LARGE_INTEGER freq = { .QuadPart = 0 };
82  if (freq.QuadPart == 0)
83  {
84      if (!QueryPerformanceFrequency(&freq))
85          throw Exception(Exception::OTHER_ERROR, "Timer: QueryPerformanceFrequency failed ..."));
86 }

hrtimer.cpp(81): error C2059: syntax error: '.'
hrtimer.cpp(81): error C2143: syntax error: missing ';' before '}'
hrtimer.cpp(82): error C2059: syntax error: 'if'
hrtimer.cpp(83): error C2143: syntax error: missing ';' before '{'
hrtimer.cpp(83): error C2447: '{': missing function header (old-style formal list?)
hrtimer.cpp(87): error C2059: syntax error: 'return'

如何在MSVC编译器中将联盟初始化为其最大成员?

How do I initialize a union to its largest member under the MSVC compiler?

这是Microsoft对LARGE_INTEGER的定义:

Here is Microsoft's definiton of LARGE_INTEGER:

#if defined(MIDL_PASS)
typedef struct _LARGE_INTEGER {
#else // MIDL_PASS
typedef union _LARGE_INTEGER {
    struct {
        DWORD LowPart;
        LONG HighPart;
    } DUMMYSTRUCTNAME;
    struct {
        DWORD LowPart;
        LONG HighPart;
    } u;
#endif //MIDL_PASS
    LONGLONG QuadPart;
} LARGE_INTEGER;

推荐答案

{ .QuadPart = 0 };在C ++中是非法的.指定的初始化程序仅C.您链接到问题的问题.

{ .QuadPart = 0 }; is illegal in C++. Designated initializers are C-only. You link to a c question.

在C ++ 03 [dcl.init.aggr]/15中:(您的联合是一个集合):

In C++03 [dcl.init.aggr]/15: (your union is an aggregate):

当使用大括号括起来的初始化程序初始化联合时,大括号应仅包含联合的第一个成员的初始化程序.

When a union is initialized with a brace-enclosed initializer, the braces shall only contain an initializer for the first member of the union.

因此,除非该成员是第一个成员,否则无法初始化最大成员".

So, it is not possible to initialize "the largest member" unless that member is the first member.

MinGW警告是虚假的. g ++曾经为= { 0 };发出警告,但是这是一个常见的习惯用法,因此他们将其固定为不再这样做.我猜你有一个稍旧的版本.

The MinGW warning is bogus. g++ used to issue warnings for = { 0 };, however that is a common idiom, so they fixed it to not do that any more. I guess you have a slightly old version.

在您的代码中,= { 0 };应将DUMMYSTRUCTNAME初始化为{0, 0}.根据,工会的所有成员都是64位的,因此在这种情况下,您实际上是在初始化最大的成员.

In your code, = { 0 }; should initialize DUMMYSTRUCTNAME to {0, 0}. According to this, all members of your union are 64-bit so in this particular case, you did actually initialize the largest member.

这篇关于使用MSVC编译器下的最大成员初始化联盟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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