當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ std::is_nothrow_move_constructible用法及代碼示例


<type_traits>頭文件中提供了C++ STL的std::is_nothrow_move_constructible模板。 C++ STL的std::is_nothrow_move_constructible模板用於檢查給定的類型T 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不是可移動構造類型。

下麵是說明C /C++中的std::is_nothrow_default_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 floar 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

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




相關用法


注:本文由純淨天空篩選整理自bansal_rtk_大神的英文原創作品 std::is_nothrow_move_constructible in C++ with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。