mercredi 16 septembre 2020

Why would trivial copy/move contructibility depend on trivial destructibility?

A class with all special functions defaulted except a non-trivial destructor is not trivially move or copy constructible. See https://godbolt.org/z/o83rPz for an example:

#include <type_traits>

class Sample
{
public:
        Sample(Sample const&) = default;
        Sample(Sample&&) = default;
        Sample& operator=(Sample const&) = default;
        Sample& operator=(Sample&&) = default;
        ~Sample() {}
};

static_assert(std::is_copy_constructible<Sample>::value, "");
static_assert(std::is_move_constructible<Sample>::value, "");
static_assert(std::is_trivially_copy_constructible<Sample>::value, ""); // Fails with GCC and Clang
static_assert(std::is_trivially_move_constructible<Sample>::value, ""); // Fails with GCC and Clang
static_assert(std::is_copy_assignable<Sample>::value, "");
static_assert(std::is_move_assignable<Sample>::value, "");
static_assert(std::is_trivially_copy_assignable<Sample>::value, "");
static_assert(std::is_trivially_move_assignable<Sample>::value, "");

Both GCC and Clang fail the corresponding assertions, while ICC passes. Strange enough, assignments are not affected, despite I could understand that the assigned-to object needs to be destroyed. But it seems to be right the other way around. Why? And why does ICC disagree?

Aucun commentaire:

Enregistrer un commentaire