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]
T 可移动构造则为 true ,否则为 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++标准。

缺陷报告 应用于 发布时行为 正确行为
LWG 2196 C++11 T && 无法形成时行为不明确 此种情况下产生的值为 false

参见

检查类型是否具有特定参数的构造函数
(类模板)
检查类型是否具有默认构造函数
(类模板)
检查类型是否具有拷贝构造函数
(类模板)
指定该类型的对象可被移动构造
(概念)
(C++11)
将实参转换为右值引用
(函数模板)
若移动构造函数不抛出异常则将实参转换为右值引用
(函数模板)