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

定义于头文件 <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 起)

1) 若 T 不是可引用类型(即可为 cv 限定的 void 或带 cv-qualifier-seqref-qualifier 的函数类型),则提供等于 false 的成员常量 value 。否则,提供等于 std::is_constructible<T, T&&>::value 的成员常量 value

2) 同 (1) ,但使用 std::is_trivially_constructible<T, T&&>

3) 同 (1) ,但使用 std::is_nothrow_constructible<T, T&&>

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
[静态]
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 ,因为否则它们就不可用于任何提供强异常保证的代码中。

多数实现中, is_nothrow_move_constructible 亦检查析构函数是否抛出,因为它等效于 noexcept(T(arg)) 。同样的情况应用于 is_trivially_move_constructible ,它在这些实现中亦要求析构函数为平凡: GCC 漏洞 51452LWG 问题 2116

示例

#include <iostream>
#include <type_traits>
 
struct Ex1 {
    std::string str; // 成员拥有非平凡而不抛出的移动构造函数
};
struct Ex2 {
    int n;
    Ex2(Ex2&&) = default; // 平凡且不抛出
};
struct NoMove {
    // 避免默认移动构造函数的隐式声明
    // 然而,该类仍为可移动构造因为复制构造函数能绑定到右值参数
    NoMove(const NoMove&) {}
};
 
int main() {
    std::cout << std::boolalpha << "Ex1 is move-constructible? "
              << std::is_move_constructible<Ex1>::value << '\n'
              << "Ex1 is trivially move-constructible? "
              << std::is_trivially_move_constructible<Ex1>::value << '\n'
              << "Ex1 is nothrow move-constructible? "
              << std::is_nothrow_move_constructible<Ex1>::value << '\n'
              << "Ex2 is trivially move-constructible? "
              << std::is_trivially_move_constructible<Ex2>::value << '\n'
              << "Ex2 is nothrow move-constructible? "
              << std::is_nothrow_move_constructible<Ex2>::value << '\n';
 
    std::cout << std::boolalpha
              << "NoMove is move-constructible? "
              << std::is_move_constructible<NoMove>::value << '\n'
              << "NoMove is nothrow move-constructible? "
              << std::is_nothrow_move_constructible<NoMove>::value << '\n';
}

输出:

Ex1 is move-constructible? true
Ex1 is trivially move-constructible? false
Ex1 is nothrow move-constructible? true
Ex2 is trivially move-constructible? true
Ex2 is nothrow move-constructible? true
NoMove is move-constructible? true
NoMove is nothrow move-constructible? false

参阅

检查对象是否实现了特定参数的构造函数
(类模板)
检查类型是否有默认构造函数
(类模板)
检查类型是否拥有复制构造函数
(类模板)
若移动构造函数不抛出则获得右值引用
(函数模板)

版本历史

  • (当前 | 先前 2017年7月7日 (五) 21:00Fruderica讨论 | 贡献. . (4,564字节) (+812). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 18:14P12bot讨论 | 贡献 . . (3,752字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 11:42P12bot讨论 | 贡献 . . (3,752字节) (-166). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 16:12P12bot讨论 | 贡献 . . (3,918字节) (+313). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月27日 (六) 11:49P12讨论 | 贡献 . . (3,605字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (3,605字节) (-37). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 03:33P12讨论 | 贡献 . . (3,642字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (3,642字节) (+3,642). . (Translated from the English version using Google Translate)