使用 g++ 静态初始化带有命名标签的嵌套 C++ 结构

Statically initialize nested C++ structures with named labels using g++

我importing/porting 将现有的 C 代码转换为 C++。我希望对现有代码主体进行尽可能少的更改,以尽量减少对现有代码的修改。 此代码使用嵌套结构的静态命名初始化。对于快速组成的示例:

Car car =
{
  .color = RED,
  .tire.tread = OLD,
  .tire.diameter = 27.106,
  .tire.material.type = RUBBER,
  .tire.material.density = 700,
};

我发现 these are called designated initializers

我了解了 GNU initializers,但我还不知道如何用它来实现层次结构。

我读过 designated initializers are supported in g++ with c++11 enabled,但它似乎对我不起作用。

我正在移植的代码包含至少有四层层次结构的初始化页面。因此,我正在尝试寻找不会涉及太多的直接 ward 转换。

我正在寻找以下可能的解决方案之一:

我使用的是 g++ 版本

g++.exe (i686-posix-dwarf-rev0, Built by MinGW-W64 project) 8.1.0

使用这种 嵌套 指定的初始化程序似乎是一种边缘情况。以下内容适用于 clang-cl(在 Visual Studio 2019 年)和本机 MSVC 编译器(但后者仅使用 /std:c++latest 选项,它使用草案 C++20标准):

struct inner {
    int x, y;
    double z;
};
struct outer {
    char a;
    double b;
    inner c;
};

outer my_outer = { .a = 'a', .b = 1.2, .c = { .x = 3, .y = 4, .z = 5.6 } };

clang-cl一起,初始化器可以简写成下面的形式:

outer my_outer = { .a = 'a', .b = 1.2, .c.x = 3, .c.y = 4, .c.z = 5.6 };

MSVC 在这种情况下抱怨:

error C7558: nested member access is not allowed in standard C++ designated initializers; use a nested initializer list

因此,对于您的示例,您可以尝试这样做:

Car car =
{
  .color = RED,
  .tire = {
     .tread = OLD,
     .diameter = 27.106,
     .material.type = RUBBER,
     .material.density = 700,
   }
};

topic on cppreference 中有一个有用的 'discussion',这部分值得注意:

...each designator must name a direct non-static data member of T, and all designators used in the expression must appear in the same order as the data members of T.