C++ 中 std::is_nothrow_move_constructible 的使用和示例
C++ STL 中的 std::is_nothrow_move_constructible 模板存在于 < type_traits>头文件中。C++ STL 中的 std::is_nothrow_move_constructible 模板用于检查给定类型 T 是否可移动构造,并且不会引发任何异常。如果 T 是可移动构造类型,则返回布尔值true;否则返回false。
头文件:
#include<type_traits>
模板类:
template <class T>
struct is_nothrow_move_constructible;
语法:
std::is_nothrow_move_constructible<T>::value
参数: 模板 std::is_nothrow_move_constructible 接受一个单一的参数 T(Trait类) 来检查 T 是否是可移动构造类型。
返回值: 该模板返回一个布尔变量,如下所示:
- True: 如果类型 T 是可移动构造类型。
- False: 如果类型 T 不是可移动构造类型。
下面是演示 std::is_nothrow_move_constructible 模板的程序:
程序:
// C++ program to illustrate
// std::is_nothrow_move_constructible
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
// Declare structures
struct B {
};
struct A {
A& operator=(A&) = delete;
};
struct C {
C(C&&) {}
};
struct D {
D(D&&) = delete;
};
// Driver Code
int main()
{
cout << boolalpha;
// Check if int is no throw move
// constructible or not
cout << "int: "
<< is_nothrow_move_constructible<int>::value
<< endl;
// Check if char is no throw move
// constructible or not
cout << "char: "
<< is_nothrow_move_constructible<char>::value
<< endl;
// Check if float is no throw move
// constructible or not
cout << "float: "
<< is_nothrow_move_constructible<float>::value
<< endl;
// Check if struct A is no throw
// move constructible or not
cout << "struct A is nothrow move constructible? "
<< is_nothrow_move_constructible<A>::value
<< endl;
// Check if struct B is no throw
// move constructible or not
cout << "struct B is nothrow move constructible? "
<< is_nothrow_move_constructible<B>::value
<< endl;
// Check if struct C is no throw
// move constructible or not
cout << "struct C is nothrow move constructible? "
<< is_nothrow_move_constructible<C>::value
<< endl;
// Check if struct D is no throw
// move constructible or not
cout << "struct D is nothrow move constructible? "
<< is_nothrow_move_constructible<D>::value
<< endl;
return 0;
}
输出:
int: true
char: true
float: true
struct A is nothrow move constructible? true
struct B is nothrow move constructible? true
struct C is nothrow move constructible? false
struct D is nothrow move constructible? false