具有可以引发的移动操作的仅移动类的示例是什么?

What's an example of a move-only class with a move operation that can throw?

本文关键字:移动 是什么 操作      更新时间:2023-10-16

std::move_if_noexcept定义如下(C 11 20.2.3/7-8):

template <class T>
typename conditional<!is_nothrow_move_constructible<T>::value
                     && is_copy_constructible<T>::value,
                     const T&,
                     T&&
                    >::typemove_if_noexcept(T& x) noexcept;
Returns: std::move(x)

用普通的英语,如果x的移动构造器不投掷,或者x不复制可构造构造,则move_if_noexcept(x)x施放到RVALUE。这使我想知道是否有任何常见类型不可复制,但其移动构造函数可能会抛出。

在标准库中,我已经检查了不可复制的类型unique_ptrfuturepromise,各种静音类型,unique_lockpackaged_task和CC_11,除非我误读了标准,否则他们都声明了他们的移动操作noexcept

标准库(C 11或C 14)或常用的第三方库(例如Boost)中是否存在仅移动类型的类型?

fstream类是移动构造函数未声明为noexcept和复制构造函数的示例:http://en.cppreference.com/w/cpp/io/basic_ofstream/basic_ofstream/basic_ofstream

/div>