Namespaces
Variants

std:: move_if_noexcept

From cppreference.net
Utilities library
ヘッダーで定義 <utility>
template < class T >
/* 下記参照 */ move_if_noexcept ( T & x ) noexcept ;
(C++11以降)
(constexprはC++14以降)

std::move_if_noexcept は、ムーブコンストラクタが例外を投げない場合、またはコピーコンストラクタが存在しない場合(ムーブ専用型)に引数への右辺値参照を取得し、それ以外の場合には引数への左辺値参照を取得します。これは通常、ムーブセマンティクスと強い例外保証を組み合わせるために使用されます。

std::move_if_noexcept の戻り値の型は:

目次

パラメータ

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に変換する
(関数テンプレート)