类型特性
类型特性定义一个编译时基于模板的结构,以查询或修改类型的属性。
试图特化定义于 <type_traits> 头文件的模板导致未定义行为,除了 std::common_type 可依照其所描述特化。
定义于<type_traits>头文件的模板可以用不完整类型实例化,除非另外有指定,尽管通常禁止以不完整类型实例化标准库模板。
受支持操作
继承自 std::integral_constant
成员常量
value [静态] | 若 T 为可移动构造则为 true ,否则为 false(公开静态成员常量) |
成员函数
operator bool | 转换对象为 bool ,返回 value (公开成员函数) |
operator() (C++14) | 返回 value (公开成员函数) |
成员类型
类型 | 定义 |
value_type | bool |
type | std::integral_constant<bool, value> |
检查类型是否能从右值引用构造
std::is_move_constructible,
std::is_trivially_move_constructible,
std::is_nothrow_move_constructible
template< class T > | (1) | (C++11 起) |
template< class T > | (2) | (C++11 起) |
template< class T > | (3) | (C++11 起) |
1) 若 T
不是可引用类型(即可为 cv 限定的 void 或带 cv-qualifier-seq 或 ref-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 > | (C++17 起) | |
template< class T > | (C++17 起) | |
template< class T > | (C++17 起) |
可能的实现
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 漏洞 51452 、 LWG 问题 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 << std::endl
<< "Ex1 is trivially move-constructible? "
<< std::is_trivially_move_constructible<Ex1>::value << std::endl
<< "Ex1 is nothrow move-constructible? "
<< std::is_nothrow_move_constructible<Ex1>::value << std::endl
<< "Ex2 is trivially move-constructible? "
<< std::is_trivially_move_constructible<Ex2>::value << std::endl
<< "Ex2 is nothrow move-constructible? "
<< std::is_nothrow_move_constructible<Ex2>::value << std::endl;
std::cout << std::boolalpha
<< "NoMove is move-constructible? "
<< std::is_move_constructible<NoMove>::value << std::endl
<< "NoMove is nothrow move-constructible? "
<< std::is_nothrow_move_constructible<NoMove>::value << std::endl;
return 0;
}