📌  相关文章
📜  C++中的std :: is_nothrow_move_constructible与示例

📅  最后修改于: 2021-05-30 14:40:07             🧑  作者: Mango

C++ STL的std :: is_nothrow_move_constructible模板位于< type_traits >头文件中。 C++ STL的std :: is_nothrow_move_constructible模板用于检查给定类型T T是否为可移动构造,并且众所周知它不会引发任何异常。如果T是可移动构造类型,则返回布尔值true,否则返回false。

头文件:

#include

模板类别:

template 
struct is_nothrow_move_constructible;

句法:

std::is_nothrow_move_constructible::value

参数:模板std :: is_nothrow_move_constructible接受单个参数T(Trait类)以检查T是否为可移动构造类型。

返回值:该模板返回一个布尔变量,如下所示:

  • 是:如果类型T是可移动构造类型。
  • False:如果类型T不是可移动构造类型。

下面的程序说明了C / C++中的std :: is_nothrow_default_constructible模板:

程序:

// C++ program to illustrate
// std::is_nothrow_move_constructible
#include 
#include 
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::value
         << endl;
  
    // Check if char is no throw move
    // constructible or not
    cout << "char: "
         << is_nothrow_move_constructible::value
         << endl;
  
    // Check if floar is no throw move
    // constructible or not
    cout << "float: "
         << is_nothrow_move_constructible::value
         << endl;
  
    // Check if struct A is no throw
    // move constructible or not
    cout << "struct A is nothrow move constructible? "
         << is_nothrow_move_constructible::value
         << endl;
  
    // Check if struct B is no throw
    // move constructible or not
    cout << "struct B is nothrow move constructible? "
         << is_nothrow_move_constructible::value
         << endl;
  
    // Check if struct C is no throw
    // move constructible or not
    cout << "struct C is nothrow move constructible? "
         << is_nothrow_move_constructible::value
         << endl;
  
    // Check if struct D is no throw
    // move constructible or not
    cout << "struct D is nothrow move constructible? "
         << is_nothrow_move_constructible::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

参考: http://www.cplusplus.com/reference/type_traits/is_nothrow_move_constructible/

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”