std::move_if_noexcept
| ヘッダ <utility>
|
||
template< class T >
/* see below */ move_if_noexcept( T& x ) noexcept;
|
で定義 (C++11以上) (C++14以上でconstexpr) |
|
std::move_if_noexcept そのムーブコンストラクタが例外を投げない場合、またはコピーコンストラクタが存在しない(ムーブオンリー型)場合に、その引数への右辺値参照を取得します。そうでなければ、その引数への左辺値参照を取得します。これは通常、強い例外保証とムーブセマンティクスを組み合わせるために使用されます。
std::move_if_noexceptの戻り値の型は次の通りです:
T&&もしstd::is_nothrow_move_constructible<T>::value || !std::is_copy_constructible<T>::valueがtrueである場合。- そうでなければ
const T&。
パラメータ
| x | - | 移動またはコピーされるオブジェクト |
戻り値
std :: move ( x ) または x 、例外保証に応じて選択します。
計算量
定数。
注記
これは例えば、
std::vector::resize
によって使用されます。これは新しいストレージを割り当て、古いストレージから新しいストレージへ要素を移動またはコピーする必要がある場合があります。この操作中に例外が発生した場合、
std::vector::resize
はこれまでに行ったすべての操作を元に戻します。これは、
std::move_if_noexcept
を使用して移動構築とコピー構築のどちらを使用するかを決定した場合にのみ可能です(コピーコンストラクタが利用できない場合は、移動コンストラクタがどちらにしても使用され、強い例外保証が放棄される可能性があります)。
例
#include <iostream> #include <utility> struct Bad { Bad() {} Bad(Bad&&) // may throw { std::cout << "Throwing move constructor called\n"; } Bad(const Bad&) // may throw as well { std::cout << "Throwing copy constructor called\n"; } }; struct Good { Good() {} Good(Good&&) noexcept // will NOT throw { std::cout << "Non-throwing move constructor called\n"; } Good(const Good&) noexcept // will NOT throw { std::cout << "Non-throwing copy constructor called\n"; } }; int main() { Good g; Bad b; [[maybe_unused]] Good g2 = std::move_if_noexcept(g); [[maybe_unused]] Bad b2 = std::move_if_noexcept(b); }
出力:
Non-throwing move constructor called Throwing copy constructor called
関連項目
|
(C++11)
|
関数の引数を転送し、テンプレート引数の型を使用してその値カテゴリを保持する
(関数テンプレート) |
|
(C++11)
|
引数をxvalueに変換する
(関数テンプレート) |