std::is_move_assignable, std::is_trivially_move_assignable, std::is_nothrow_move_assignable
來自 cppreference.com
定義於標頭檔案 <type_traits> |
||
template< class T > struct is_move_assignable; |
(1) | (C++11 起) |
template< class T > struct is_trivially_move_assignable; |
(2) | (C++11 起) |
template< class T > struct is_nothrow_move_assignable; |
(3) | (C++11 起) |
型別特徵 | 成員常量 value 的值 | |
---|---|---|
T 是一個可引用型別 |
T 不是可引用型別 | |
(1) | std::is_assignable<T&, T&&>::value | false |
(2) | std::is_trivially_assignable<T&, T&&>::value | |
(3) | std::is_nothrow_assignable<T&, T&&>::value |
如果 T
不是完整型別,(可能 cv 限定的) void,或未知邊界陣列,則行為未定義。
如果上述模板的例項化直接或間接依賴於不完整型別,並且該例項化在該型別假設完成時可能產生不同的結果,則行為未定義。
如果程式為此頁上描述的任何模板新增特化,則行為未定義。
目錄 |
[編輯] 輔助變數模板
template< class T > inline constexpr bool is_move_assignable_v = |
(C++17 起) | |
template< class T > inline constexpr bool is_trivially_move_assignable_v = |
(C++17 起) | |
template< class T > inline constexpr bool is_nothrow_move_assignable_v = |
(C++17 起) | |
繼承自 std::integral_constant
成員常量
value [靜態] |
如果 T 是可移動賦值的,則為 true,否則為 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_assignable : std::is_assignable<typename std::add_lvalue_reference<T>::type, typename std::add_rvalue_reference<T>::type> {}; template<class T> struct is_trivially_move_assignable : std::is_trivially_assignable<typename std::add_lvalue_reference<T>::type, typename std::add_rvalue_reference<T>::type> {}; template<class T> struct is_nothrow_move_assignable : std::is_nothrow_assignable<typename std::add_lvalue_reference<T>::type, typename std::add_rvalue_reference<T>::type> {}; |
[編輯] 注意
特質 std::is_move_assignable
比 MoveAssignable 寬鬆,因為它不檢查賦值結果的型別(對於 MoveAssignable 型別,結果必須是 T&
),也不檢查賦值後目標值與賦值前源值等價的語義要求。
型別不必實現移動賦值運算子即可滿足此特質;詳見MoveAssignable。
[編輯] 示例
執行此程式碼
#include <iostream> #include <string> #include <type_traits> struct Foo { int n; }; struct NoMove { // prevents implicit declaration of default move assignment operator // however, the class is still move-assignable because its // copy assignment operator can bind to an rvalue argument NoMove& operator=(const NoMove&) { return *this; } }; int main() { std::cout << std::boolalpha << "std::string is nothrow move-assignable? " << std::is_nothrow_move_assignable<std::string>::value << '\n' << "int[2] is move-assignable? " << std::is_move_assignable<int[2]>::value << '\n' << "Foo is trivially move-assignable? " << std::is_trivially_move_assignable<Foo>::value << '\n' << "NoMove is move-assignable? " << std::is_move_assignable<NoMove>::value << '\n' << "NoMove is nothrow move-assignable? " << std::is_nothrow_move_assignable<NoMove>::value << '\n'; }
輸出
std::string is nothrow move-assignable? true int[2] is move-assignable? false Foo is trivially move-assignable? true NoMove is move-assignable? true NoMove is nothrow move-assignable? false
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 2196 | C++11 | 如果無法形成 T&&,行為不明確 | 在這種情況下,生成的值為 false |
[編輯] 參閱
(C++11)(C++11)(C++11) |
檢查型別是否具有針對特定引數的賦值運算子 (類模板) |
(C++11)(C++11)(C++11) |
檢查型別是否具有複製賦值運算子 (類模板) |