gpt4 book ai didi

c++ - 具有未初始化成员的结构的 constexpr 默认构造函数仅在模板化时有效

转载 作者:太空狗 更新时间:2023-10-29 23:13:46 28 4
gpt4 key购买 nike

似乎我可以显式默认一个带有未初始化成员的 constexpr 默认构造函数当且仅当该结构是一个模板。即使实例化模板结构也是如此。

这是正确的吗?

示例:

struct SFoo
{
constexpr SFoo() =default; // disallowed
int x;
};

template <typename tTYPE>
struct SBar
{
constexpr SBar() =default; // accepted
int x;
};

int main(int argc, char const* const argv[])
{
SBar<int> bar; // also accepted
constexpr SBar<int> bar2; // disallowed (correctly)

return 0;
}

LLVM 输出:

3:3: error: defaulted definition of default constructor is not constexpr
constexpr SFoo() =default;

LLVM 和 GCC 都拒绝 SFoo 并接受 SBar

最佳答案

if and only if

让我给你数学家的答案。不。尝试将 IIF 应用于 1300 页的文档是一个 NP 难题。

Is this correct?

不管怎样你的程序都是无效的。

[dcl.init/7] If a program calls for the default initialization of an object of a const-qualified type T, T shall be a class type with a user-provided default constructor.

是否是模板无关紧要。

关于c++ - 具有未初始化成员的结构的 constexpr 默认构造函数仅在模板化时有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37245834/

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