gpt4 book ai didi

c++ - 具有多个默认构造函数的普通类的示例?

转载 作者:行者123 更新时间:2023-12-01 14:07:02 26 4
gpt4 key购买 nike

C++17 标准的 §13 说:

"A trivial class is a class that is trivially copyable and has one or more default constructors (15.1), all of which are either trivial or deleted and at least one of which is not deleted."


我没有想出一个很好的例子来说明这样的措辞是有意义的。什么是“至少一个未被删除的”部分?。一个普通类的构造函数集是否可以包含多个“未删除”的默认构造函数?如果是,那么这种“未删除”的构造函数一定是微不足道的(根据上述措辞),但这似乎是不可能的。

最佳答案

例子:

struct Foo
{
Foo() = default; // #1 default constructor, trivial, not deleted

template <class... Args>
Foo(Args...) = delete; // #2 default constructor, deleted
};

static_assert(std::is_trivial_v<Foo>);

auto test()
{
Foo f{}; // OK
}

has one or more default constructors


#1 和 #2 都是默认构造函数(参见 [class.default.ctor])

all of which are either trivial or deleted


#1 是微不足道的,#2 被删除了

and at least one of which is not deleted


#1 未删除

[class.default.ctor]

A default constructor for a class X is a constructor of class X forwhich each parameter that is not a function parameter pack has adefault argument (including the case of a constructor with noparameters). [...]

关于c++ - 具有多个默认构造函数的普通类的示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63286205/

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