我目前正在使用遗留的 C++ 代码,已成功通过 gcc 2.9.X 编译.
我被要求将此遗留代码移植到 gcc 3.4.X.大多数错误很容易纠正,但这个特别的错误让我感到困惑.
I'm currently working with legacy C++ code, successfully compiled with gcc 2.9.X.
I've been asked to port this legacy code to gcc 3.4.X. Most of the errors were easily corrected, but this particular one puzzles me.
上下文:
struct TMessage
{
THeader header;
TData data;
};
struct THeader
{
TEnum myEnum;
TBool validity;
};
做了什么:
const TMessage init = {{0}};
/* Later in the code ... */
TMessage message = init;
我的问题:
{{}} 运算符的含义是什么?它是否将第一个字段(header)初始化为二进制 0 ?它是否将第一个结构(enum)的第一个字段初始化为(文字)0?
My question(s) :
What is the meaning of the {{}} operator ? Does it initialize the first field (the header) to a binary 0 ? Does it initialize the first field of the first structure (the enum) to (literal) 0 ?
我得到的 3.4.6 错误是 从 'int' 到 'TEnum' 的无效转换
,带有一对或两对大括号.
The 3.4.6 error I get is invalid conversion from 'int' to 'TEnum'
, either with one or two pairs of curly brackets.
如何在不使用 memset 的情况下将我的结构设置为一堆 0?
How can I set my structure to a bunch of 0's without using memset ?
提前致谢.
它将 POD 结构的所有字段初始化为 0.
It initialises all fields of the POD structure to 0.
理由:
const SomeStruct init = {Value};
将 SomeStruct 的第一个字段初始化为 Value,将结构的其余部分初始化为零(我忘记了标准中的部分,但它在某处)
Initialises the first field of SomeStruct to Value, the rest of the structure to zero (I forget the section in the standard, but it's there somewhere)
因此:
const SomeOtherStruct init = {{Value}};
将结构的第一个字段(其中结构的第一个字段本身就是一个 POD 结构)的第一个字段初始化为 Value,将第一个字段的其余部分初始化为零,将结构的其余部分初始化为 0.
Initialises the first field of the first field of the structure (where the first field of the structure is itself a POD struct) to Value, and the rest of the first field to zero, and the rest of the structure to 0.
此外,这只是行不通,因为 C++ 禁止将 int
隐式转换为枚举类型,因此您可以这样做:
Additionally, this only isn't working because c++ forbids implicit conversion of int
to enum types, so you could do:
const SomeOtherStruct init = {{TEnum(0)}};
这篇关于双花括号初始化 C 结构的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!