名稱空間
變體
操作

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

來自 cppreference.com
< cpp‎ | 型別
 
 
超程式設計庫
型別特性
型別類別
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11) 
(C++11)
(C++11)
型別屬性
(C++11)
(C++11)
(C++14)
(C++11)(C++26 中已棄用)
(C++11)(直到 C++20*)
(C++11)(C++20 中已棄用)
(C++11)
型別特性常量
元函式
(C++17)
支援的操作
關係與屬性查詢
型別修改
(C++11)(C++11)(C++11)
型別轉換
(C++11)(C++23 中已棄用)
(C++11)(C++23 中已棄用)
(C++11)
(C++11)(直到 C++20*)(C++17)

(C++11)
(C++17)
編譯時有理數算術
編譯時整數序列
 
定義於標頭檔案 <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

成員常量

[靜態]
trueT 可移動構造,否則為 false
(public static 成員常量)

成員函式

operator bool
將物件轉換為 bool,返回 value
(公開成員函式)
operator()
(C++14)
返回 value
(公開成員函式)

成員型別

型別 定義
value_type bool
型別 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 51452LWG issue 2116

[編輯] 示例

#include <string>
#include <type_traits>
 
struct Ex1
{
    std::string str; // member has a non-trivial but non-throwing move constructor
};
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; // trivial and non-throwing
};
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
{
    // prevents implicit declaration of default move constructor;
    // however, the class is still move-constructible because its
    // copy constructor can bind to an rvalue argument
    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
{
    // Not move-constructible since the lvalue reference
    // can't bind to the rvalue argument
    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++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 2196 C++11 如果無法形成 T&&,則行為不明確 在這種情況下,生成的值為 false

[編輯] 參閱

檢查型別是否具有針對特定引數的建構函式
(類模板) [編輯]
檢查型別是否具有預設建構函式
(類模板) [編輯]
檢查型別是否具有複製建構函式
(類模板) [編輯]
指定型別的物件可以被移動構造
(概念) [編輯]
(C++11)
將引數轉換為亡值
(函式模板) [編輯]
如果移動建構函式不丟擲異常,則將引數轉換為亡值
(函式模板) [編輯]