std::is_move_assignable, std::is_trivially_move_assignable, std::is_nothrow_move_assignable
来自 cppreference.cn
定义于头文件 <type_traits> |
||
template< class T > struct is_move_assignable; |
(1) | (since C++11) |
template< class T > struct is_trivially_move_assignable; |
(2) | (since C++11) |
template< class T > struct is_nothrow_move_assignable; |
(3) | (since 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 = |
(since C++17) | |
template< class T > inline constexpr bool is_trivially_move_assignable_v = |
(since C++17) | |
template< class T > inline constexpr bool is_nothrow_move_assignable_v = |
(since C++17) | |
继承自 std::integral_constant
成员常量
value [静态] |
true 如果 T 是可移动赋值的,false 否则为 false(公共静态成员常量) |
成员函数
operator bool |
将对象转换为 bool,返回 value (公共成员函数) |
operator() (C++14) |
返回 value (公共成员函数) |
成员类型
类型 | 定义 |
value_type
|
bool |
type
|
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++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 2196 | C++11 | 如果无法形成 T&&,则行为不明确 | 在这种情况下,产生的值为 false |
[编辑] 参见
(C++11)(C++11)(C++11) |
检查类型是否具有针对特定参数的赋值运算符 (class template) |
(C++11)(C++11)(C++11) |
检查类型是否具有复制赋值运算符 (class template) |