【问题标题】:Why would trivial copy/move contructibility depend on trivial destructibility?为什么微不足道的复制/移动可构造性取决于微不足道的可破坏性?
【发布时间】:2021-01-03 09:59:45
【问题描述】:

除了非平凡的析构函数外,所有特殊函数都默认的类不是平凡的移动或复制构造。示例见https://godbolt.org/z/o83rPz

#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, "");

GCC 和 Clang 均未通过相应的断言,而 ICC 通过。奇怪的是,分配不受影响,尽管我可以理解分配对象需要被销毁。但反过来似乎是对的。为什么? ICC 为何不同意?

【问题讨论】:

标签: c++ c++11 trivially-copyable


【解决方案1】:

我看到 cmets 已经提到它了。答案在此处的注释部分https://en.cppreference.com/w/cpp/types/is_copy_constructible

同样适用于is_trivially_copy_constructible,在这些实现中,它还要求析构函数是微不足道的:GCC bug 51452LWG issue 2116

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-04
    • 1970-01-01
    • 2017-01-30
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 2017-10-21
    相关资源
    最近更新 更多