gpt4 book ai didi

c++ - S 不是文字类型吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:19:20 24 4
gpt4 key购买 nike

此代码无法在 Coliru 中编译,尽管它似乎符合 iso §5.19 p2 第 9 个要点和 iso §3.9 p10,即 S 是文字类型,因此 S a(1);应该被认为是一个常量表达式。特别是,iso §3.9 p10 bullet point 3 没有提及任何关于非初始化成员的内容。

#include <iostream>

struct S
{
int i;
float x;

constexpr S(int j) : i{j} {}
};

int main()
{
constexpr S a(1);
}

最佳答案

这是在 7.1.5/4 中标准化的([dcl.constexpr],“constexpr 说明符”):

The definition of a constexpr constructor shall satisfy the following constraints:

  • [...]

  • every non-static data member and base class sub-object shall be initialized

  • [...]

关于c++ - S 不是文字类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20744526/

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