小编Mat*_*059的帖子

为什么`std:variant`的`operator =(T && t)`的noexcept规范不依赖于内部类型的析构函数的noexcept规范?

详题:为什么std:variantoperator=(T&& t)的noexcept规范不依赖于内部类型的析构函数的noexcept规范?

我可以在cppreference上看到

template <class T> variant& operator=(T&& t) noexcept(/* see below */);
Run Code Online (Sandbox Code Playgroud)

noexcept(std::is_nothrow_assignable_v<T_j&, T> && 
std::is_nothrow_constructible_v<T_j, T>)
Run Code Online (Sandbox Code Playgroud)

所以这个编译:

struct FooThrow {
  ~FooThrow() noexcept(false) {throw;} 
};
static_assert(std::is_nothrow_assignable_v<std::variant<FooThrow, int>, int>);
Run Code Online (Sandbox Code Playgroud)

但它调用FooThrow的析构函数是noexcept(false):

std::variant<FooThrow, int> x;
x = 3; // throws
Run Code Online (Sandbox Code Playgroud)

这似乎不对.我错过了什么吗?

c++ variant nothrow std-variant

3
推荐指数
1
解决办法
58
查看次数

标签 统计

c++ ×1

nothrow ×1

std-variant ×1

variant ×1