命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | types
 
 
元编程库
类型特征
类型类别
(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

成员常量

value
[静态]
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; // 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++ 标准。

DR 应用于 已发布行为 正确行为
LWG 2196 C++11 如果无法形成 T&&,则行为不明确 在这种情况下,产生的值为 false

[编辑] 参见

检查类型是否具有特定参数的构造函数
(类模板) [编辑]
检查类型是否具有默认构造函数
(类模板) [编辑]
检查类型是否具有复制构造函数
(类模板) [编辑]
指定类型的对象可以进行移动构造
(概念) [编辑]
(C++11)
将参数转换为 xvalue
(函数模板) [编辑]
如果移动构造函数不抛出异常,则将参数转换为 xvalue
(函数模板) [编辑]