哪项操作在这里不是琐碎的

Which operation is not trivial here?

本文关键字:操作 在这里      更新时间:2024-09-28

编译器同意,下面的XY是默认的可构造的,但不是微不足道的(演示(。

#include <type_traits>
struct X { int x {}; };
struct Y { int y = 0; };
static_assert(std::is_default_constructible_v<X>);
static_assert(std::is_default_constructible_v<Y>);
static_assert(!std::is_trivially_default_constructible_v<X>);
static_assert(!std::is_trivially_default_constructible_v<Y>);

为什么它们不是琐碎的?根据cppreference.com(请参阅is_trivially_constructible(,在默认构造期间必须调用一个非平凡的操作。那是哪一个?

https://en.cppreference.com/w/cpp/language/default_constructor#Trivial_default_constructor说:

如果以下所有情况都为真,则类T的默认构造函数是琐碎的(即不执行任何操作(:

  • T没有具有默认初始值设定项的非静态成员(从C++11开始(