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

标签 c++

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 for which each parameter that is not a function parameter pack has a default argument (including the case of a constructor with no parameters). [...]

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

相关文章:

c++ - 带参数的 QT 信号如何工作

C++ 编译时位掩码添加

c++ - Eigen::Quaternion FromTwoVectors() 不返回有效的四元数

c++ 将 2 个 uint8_t 组合成一个 uint16_t 不起作用?

c++ - 锁定多个互斥锁

c++ - cvLogPolar 导致 opencv 错误 "Bad flag (parameter or structure field)"

c++ - 如何避免来自 C 中的#define 的名称冲突? (或 C++)

c++ - WebRTC 点对点连接

c++ - Hadoop winultils 项目构建失败 - libwinutils.c WIDEN_STRING(x) 宏中的错误

c++ - 免费的 C/C++ PDF 创建器库 Linux(不是 libharu)