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


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

<type_traits>頭文件中提供了C++ STL的std::is_nothrow_move_assignable模板。 C++ STL的std::is_nothrow_move_assignable模板用於檢查T是否為可移動分配的類型,並且眾所周知它不會引發任何異常。如果T是移動可分配類型,則返回布爾值true,否則返回false。

頭文件:

#include<type_traits>

模板類別:

template< class T >
struct is_move_assignable;

用法:

std::is_move_assignable<T>::value 

參數:模板std::is_nothrow_move_assignable接受單個參數T(Trait類),以檢查T是否可無異常地移動分配。



返回值:模板std::is_nothrow_move_assignable返回一個布爾變量,如下所示:

  • True:如果類型T是移動可分配類型。
  • False:如果類型T不是可移動分配的類型。

下麵是演示std::is_nothrow_move_assignable的程序:

程序:

// C++ program to illustrate 
// std::is_nothrow_move_assignable 
#include <bits/stdc++.h> 
#include <type_traits> 
using namespace std; 
  
// Declare structures 
struct A { 
}; 
  
struct B { 
    B& operator=(B&) = delete; 
}; 
  
struct Ex1 { 
    Ex1() {} 
    Ex1(Ex1&&) 
    { 
        cout << "Throwing move constructor!"; 
    } 
    Ex1(const Ex1&) 
    { 
        cout << "Throwing copy constructor!"; 
    } 
}; 
  
struct Ex2 { 
    Ex2() {} 
    Ex2(Ex2&&) noexcept 
    { 
        cout << "Non-throwing move constructor!"; 
    } 
    Ex2(const Ex2&) noexcept 
    { 
        cout << "Non-throwing copy constructor!"; 
    } 
}; 
  
// Driver Code 
int main() 
{ 
    cout << boolalpha; 
  
    // Check if int is a move 
    // assignable or not 
    cout << "int:"
         << is_nothrow_move_assignable<int>::value 
         << endl; 
  
    // Check if struct A is a move 
    // assignable or not 
    cout << "struct A:"
         << is_nothrow_move_assignable<A>::value 
         << endl; 
  
    // Check if struct B is a move 
    // assignable or not 
    cout << "struct B:"
         << is_nothrow_move_assignable<B>::value 
         << endl; 
  
    // Check if struct Ex1 is a move 
    // assignable or not 
    cout << "struct Ex1:"
         << is_nothrow_move_assignable<Ex1>::value 
         << endl; 
  
    // Check if struct Ex2 is a move 
    // assignable or not 
    cout << "struct Ex2:"
         << is_nothrow_move_assignable<Ex2>::value 
         << endl; 
    return 0; 
}
輸出:
int:true
struct A:true
struct B:false
struct Ex1:false
struct Ex2:false

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




相關用法


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