Namespaces
Variants

std:: is_move_constructible, std:: is_trivially_move_constructible, std:: is_nothrow_move_constructible

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
ヘッダーで定義 <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以降)
型特性 メンバ定数 value の値
T 参照可能な型 の場合 T が参照可能な型でない場合
(1) std:: is_constructible < T, T && > :: value false
(2) std:: is_trivially_constructible < T, T && > :: value
(3) std:: is_nothrow_constructible < T, T && > :: value

T が完全型でない場合、(おそらくcv修飾された) void または不明な境界の配列である場合、動作は未定義です。

上記のテンプレートのインスタンス化が、直接的または間接的に不完全型に依存しており、その型が仮想的に完成された場合に異なる結果をもたらす可能性がある場合、動作は未定義です。

プログラムがこのページで説明されているテンプレートのいずれかに対する特殊化を追加する場合、動作は未定義です。

目次

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

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から継承

メンバ定数

value
[static]
true T がムーブ構築可能な場合 false それ以外の場合
(公開静的メンバ定数)

メンバ関数

operator bool
オブジェクトを bool に変換し、 value を返す
(公開メンバ関数)
operator()
(C++14)
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指定されます。なぜなら、そうでなければ強い例外保証を提供するコードでは使用できないからです。

多くの実装では、 std::is_nothrow_move_constructible はデストラクタが例外を投げるかどうかもチェックします。これは実質的に noexcept ( T ( arg ) ) と同等であるためです。同じことが std::is_trivially_move_constructible にも適用され、これらの実装ではデストラクタがトリビアルであることも要求されます: GCC bug 51452 LWG issue 2116

#include <string>
#include <type_traits>
struct Ex1
{
    std::string str; // メンバーは非トリビアルだが非スローなムーブコンストラクタを持つ
};
static_assert(std::is_move_constructible_v<Ex1>);
static_assert(!std::is_trivially_move_constructible_v<Ex1>);
static_assert(std::is_nothrow_move_constructible_v<Ex1>);
struct Ex2
{
    int n;
    Ex2(Ex2&&) = default; // トリビアルかつ非スロー
};
static_assert(std::is_move_constructible_v<Ex2>);
static_assert(std::is_trivially_move_constructible_v<Ex2>);
static_assert(std::is_nothrow_move_constructible_v<Ex2>);
struct NoMove1
{
    // デフォルトムーブコンストラクタの暗黙的な宣言を妨げる
    // ただし、コピーコンストラクタが右辺値引数にバインドできるため
    // このクラスは依然としてムーブ構築可能
    NoMove1(const NoMove1&) {}
};
static_assert(std::is_move_constructible_v<NoMove1>);
static_assert(!std::is_trivially_move_constructible_v<NoMove1>);
static_assert(!std::is_nothrow_move_constructible_v<NoMove1>);
struct NoMove2
{
    // 左辺値参照が右辺値引数にバインドできないため
    // ムーブ構築不可能
    NoMove2(NoMove2&) {}
};
static_assert(!std::is_move_constructible_v<NoMove2>);
static_assert(!std::is_trivially_move_constructible_v<NoMove2>);
static_assert(!std::is_nothrow_move_constructible_v<NoMove2>);
int main() {}

欠陥報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 2196 C++11 T && が形成できない場合の動作が不明確であった この場合の生成値は false である

関連項目

特定の引数に対するコンストラクタが型に存在するかチェックする
(クラステンプレート)
デフォルトコンストラクタが型に存在するかチェックする
(クラステンプレート)
コピーコンストラクタが型に存在するかチェックする
(クラステンプレート)
型のオブジェクトがムーブ構築可能であることを指定する
(コンセプト)
(C++11)
引数をxvalueに変換する
(関数テンプレート)
ムーブコンストラクタが例外を投げない場合に引数をxvalueに変換する
(関数テンプレート)