【发布时间】:2020-08-20 17:52:03
【问题描述】:
还有其他关于初始化嵌套结构数组的帖子。但是,遵循 Stackoverflow 其他地方的给定建议和指向 Aggregate Initialization 的链接并不能帮助我解决问题。
我的上下文是我在 Vulkan 图形 API 中初始化许多嵌套结构。
但是,在查看其他代码进行解释时,我遇到了以下“C”初始化(请注意,我正在添加定义的结构和相关代码)
Vulkan 类型定义
typedef union VkClearColorValue {
float float32[4];
int32_t int32[4];
uint32_t uint32[4];
} VkClearColorValue;
typedef struct VkClearDepthStencilValue {
float depth;
uint32_t stencil;
} VkClearDepthStencilValue;
typedef union VkClearValue {
VkClearColorValue color;
VkClearDepthStencilValue depthStencil;
} VkClearValue;
有问题的“C”代码
const VkClearValue clear_values[2] = {
[0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
[1] = {.depthStencil = {demo->depthStencil, 0}},
};
“C”表示在 C99 和最新的 gcc 编译器中是合法的。
但是,当我将它应用到 C++ 时,我得到了错误。
回到聚合初始化的规则,“C”表示在 C++17 中应该是合法的。但是,在编译时,我得到了错误:error: expected primary-expression before '.'令牌
显然我的假设是错误的。因此,谁能提供有关如何在 C++17 中使其合法的指导。
【问题讨论】:
标签: c++ struct nested initialization