考虑 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
可能是 true
或 false
)std::latch
)std::latch
具有非平凡的析构函数)?
最佳答案
这是实现自由。 C++ 标准定义了类,类的实现取决于实现。
在某些类中,标准明确规定了一个简单的析构函数。例如,如果现有的类是可简单破坏的,那么它的 std::optional
还有must be微不足道的可破坏的。这需要说明。
因此,除非某处明确声明该类是或不是可简单构造的,否则这取决于实现(在可能的情况下)。
查看 gcc 的头文件:它不仅声明,而且明确定义了析构函数:
~latch() = default;
基于这一点,编译器可以计算出整个事情是微不足道的。
关于c++ - 如果标准指定它有一个析构函数,类应该不是可简单破坏的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69334861/