gpt4 book ai didi

c++ - 这种类型的初始化叫什么?

转载 作者:行者123 更新时间:2023-12-04 00:12:50 24 4
gpt4 key购买 nike

我在 Vulkan 教程中看到了这样的代码:

VkImageMemoryBarrier imageMemoryBarrier = {

.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
.dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
.oldLayout = VK_IMAGE_LAYOUT_GENERAL,
.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
/* .image and .subresourceRange should identify image subresource accessed */
};

如果我只是列出了没有成员“.member_name =”的枚举,那将是聚合初始化,对吗?但这叫什么?这个类在 .srcAcgressMask 之前还有其他成员,我假设所有其他未提及的成员都是零初始化的?但是,如果我不知道它叫什么,那么查找它有点困难。另外,我记得这是一个新的 C 功能,但它在 Visual Studio 中为我在 C++ 中编译。这是一个 C++ 特性,对吧?

最佳答案

这仍然是聚合初始化,语法为 designated initializers从 C++20 开始支持。

T object = { .designator = arg1 , .designator { arg2 } ... }; (3) (since C++20)
T object { .designator = arg1 , .designator { arg2 } ... }; (4) (since C++20)

关于初始化器中未提及的成员:

For a non-union aggregate, elements for which a designated initializer is not provided are initialized the same as described above for when the number of initializer clauses is less than the number of members (default member initializers where provided, empty list-initialization otherwise):

顺便说一句:C++ 中的规则与 C 不同。

Note: out-of-order designated initialization, nested designated initialization, mixing of designated initializers and regular initializers, and designated initialization of arrays are all supported in the C programming language, but are not allowed in C++.

关于c++ - 这种类型的初始化叫什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67353526/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com