c++ - 如果标准指定它有一个析构函数,类应该不是可简单破坏的吗?

标签 c++ language-lawyer c++20 stdthread

考虑 std::latch [thread.latch.class] :

namespace std {
  class latch {
  public:
    static constexpr ptrdiff_t max() noexcept;

    constexpr explicit latch(ptrdiff_t expected);
    ~latch();

    latch(const latch&) = delete;
    latch& operator=(const latch&) = delete;

    void count_down(ptrdiff_t update = 1);
    bool try_wait() const noexcept;
    void wait() const;
    void arrive_and_wait(ptrdiff_t update = 1);

  private:
    ptrdiff_t counter;  // exposition only
  };
}
请注意,存在析构函数。这意味着这应该成立:
static_assert(std::is_trivially_destructible_v<std::latch> == false);
但是,实现(MSVC STL、libstdc++、libc++)则相反:https://godbolt.org/z/6s8173zTc
这是:
  • 实现自由(实现正确,is_trivially_destructible_v 可能是 truefalse )
  • 每个实现中的实现缺陷(实现应该有非平凡的析构函数 std::latch)
  • 标准缺陷(标准不应该指定 std::latch 具有非平凡的析构函数)

  • ?

    最佳答案

    这是实现自由。 C++ 标准定义了类,类的实现取决于实现。
    在某些类中,标准明确规定了一个简单的析构函数。例如,如果现有的类是可简单破坏的,那么它的 std::optional还有must be微不足道的可破坏的。这需要说明。
    因此,除非某处明确声明该类是或不是可简单构造的,否则这取决于实现(在可能的情况下)。
    查看 gcc 的头文件:它不仅声明,而且明确定义了析构函数:

    ~latch() = default;
    
    基于这一点,编译器可以计算出整个事情是微不足道的。

    关于c++ - 如果标准指定它有一个析构函数,类应该不是可简单破坏的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69334861/

    相关文章:

    c++ - std::cin输入带有空格?

    c++ - 如何定义返回 bool 的函数的概念

    c++ - 将 c++ 头文件转换为 protobuf .proto 文件

    c++ - C/C++ 的多数据库库有哪些替代方案?

    c++ - std::ignore 的要求

    c++ - 标准对 char 数组作为模板参数有什么看法?

    c++ - 使用带有std::jthread的静态库进行段错误(g++-10)

    c++ - 使用 C++ 概念实现强类型的特征

    c++ - nodejs 插件异步回调与 libuv

    c++ - 编译器是否允许优化私有(private)数据成员?