constexpr 默认默认构造函数

2024-04-11

如果我想声明我的 Clang 3.8 和 GCC 5.3,我会收到编译器错误default-ed 默认构造函数为constexpr。根据this https://stackoverflow.com/questions/20810378/should-i-mark-a-compiler-generated-constructor-as-constexprstackoverflow 问题它应该可以正常工作:

struct A
{
    constexpr A() = default;

    int x;
};

however:

Error: defaulted definition of default constructor is not constexpr

你知道到底发生了什么吗?


目前,x 仍未初始化,因此无法在编译时构造该对象。

您需要初始化x:

struct A
{
    constexpr A() = default;

    int x = 1;
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

constexpr 默认默认构造函数 的相关文章

随机推荐