◐ Shell
clean mode source ↗

std::is_move_constructible, std::is_trivially_move_constructible, std::is_nothrow_move_constructible - cppreference.com

提供: cppreference.com

<tbody> </tbody>

ヘッダ <type_traits> で定義

template< class T > struct is_move_constructible;

(1) (C++11以上)

template< class T > struct is_trivially_move_constructible;

(2) (C++11以上)

template< class T > struct is_nothrow_move_constructible;

(3) (C++11以上)

1) T が参照可能な型でなければ (つまり、 void、 cv 修飾された voidcv-qualifier-seq または ref-qualifier 付きの関数型)、 false に等しいメンバ定数 value が提供されます。 そうでなければ、 std::is_constructible<T, const T&&>::value に等しいメンバ定数 value が提供されます。

2) (1) と同じですが、 std::is_trivially_constructible<T, T&&> が使用されます。

3) (1) と同じですが、 std::is_nothrow_constructible<T, T&&> が使用されます。

T は完全型 (またはその cv 修飾された型)、 void、またはサイズの未知な配列でなければなりません。 そうでなければ、動作は未定義です。

上記のテンプレートの実体化が直接または間接的に不完全型に依存しており、もしその型が仮に完全型であったならばその実体化が異なる結果を産むであろう場合は、動作は未定義です。

このページで説明しているテンプレート に対して特殊化を追加するプログラムは未定義です。

ヘルパー変数テンプレート

<tbody> </tbody>

template< class T > inline constexpr bool is_move_constructible_v = is_move_constructible<T>::value;

(C++17以上)

template< class T > inline constexpr bool is_trivially_move_constructible_v = is_trivially_move_constructible<T>::value;

(C++17以上)

template< class T > inline constexpr bool is_nothrow_move_constructible_v = is_nothrow_move_constructible<T>::value;

(C++17以上)

std::integral_constant から継承

メンバ定数

T がムーブ構築可能であるならば true、そうでなければ false
(パブリック静的メンバ定数)

メンバ関数

オブジェクトを bool に変換します。 value を返します
(パブリックメンバ関数)
value を返します
(パブリックメンバ関数)

メンバ型

定義
value_type bool
type std::integral_constant<bool, value>

実装例

template<class T>
struct is_move_constructible :
      std::is_constructible<T, typename std::add_rvalue_reference<T>::type> {};

template<class T>
struct is_trivially_move_constructible :
     std::is_trivially_constructible<T, typename std::add_rvalue_reference<T>::type> {};

template<class T>
struct is_nothrow_move_constructible :
     std::is_nothrow_constructible<T, typename std::add_rvalue_reference<T>::type> {};

ノート

ムーブコンストラクタを持たないけれども、 const T& 型を受理するコピーコンストラクタを持つ型は、 std::is_move_constructible を満たします。

ムーブコンストラクタは通常 noexcept です。 そうでなければ、強い例外保証を提供するコードでは使用できません。

多くの処理系では、 is_nothrow_move_constructible は、実質的に noexcept(T(arg)) であるため、デストラクタが例外を投げるかどうかも確認します。 同じことが is_trivially_move_constructible にも適用され、これらの処理系では、デストラクタがトリビアルであることも要求されます。 GCC bug 51452LWG issue 2116 も参照してください。

#include <iostream>
#include <type_traits>

struct Ex1 {
    std::string str; // member has a non-trivial but non-throwing move ctor
};
struct Ex2 {
    int n;
    Ex2(Ex2&&) = default; // trivial and non-throwing
};
struct NoMove {
    // prevents implicit declaration of default move constructor
    // however, the class is still move-constructible because its
    // copy constructor can bind to an rvalue argument
    NoMove(const NoMove&) {}
};

int main() {
    std::cout << std::boolalpha << "Ex1 is move-constructible? "
              << std::is_move_constructible<Ex1>::value << '\n'
              << "Ex1 is trivially move-constructible? "
              << std::is_trivially_move_constructible<Ex1>::value << '\n'
              << "Ex1 is nothrow move-constructible? "
              << std::is_nothrow_move_constructible<Ex1>::value << '\n'
              << "Ex2 is trivially move-constructible? "
              << std::is_trivially_move_constructible<Ex2>::value << '\n'
              << "Ex2 is nothrow move-constructible? "
              << std::is_nothrow_move_constructible<Ex2>::value << '\n';

    std::cout << std::boolalpha
              << "NoMove is move-constructible? "
              << std::is_move_constructible<NoMove>::value << '\n'
              << "NoMove is nothrow move-constructible? "
              << std::is_nothrow_move_constructible<NoMove>::value << '\n';
}

出力:

Ex1 is move-constructible? true
Ex1 is trivially move-constructible? false
Ex1 is nothrow move-constructible? true
Ex2 is trivially move-constructible? true
Ex2 is nothrow move-constructible? true
NoMove is move-constructible? true
NoMove is nothrow move-constructible? false

関連項目