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


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

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

頭文件:

#include<type_traits>

模板類別:

template< class T >
struct is_nothrow_copy_assignable;

用法:

std::is_nothrow_copy_assignable< datatype >::value << '\n'

參數:模板std::is_nothrow_copy_assignable接受單個參數T(特質類)以檢查T是否為is_nothrow_copy_assignable類型。



返回值:

  • True:如果給定的數據類型T是可分配的Noh​​row副本。
  • False:如果給定的數據類型T不是可分配的Noh​​row副本。

下麵是演示C++中std::is_nothrow_copy_assignable的程序:

程序:

// C++ program to illustrate 
// std::is_nothrow_copy_assignable 
#include <bits/stdc++.h> 
#include <type_traits> 
using namespace std; 
  
// Declare structures X and Y 
struct X { 
}; 
  
struct Y { 
    Y& operator=(const Y&) 
    { 
        return *this; 
    } 
}; 
  
// Declare classes 
class A { 
}; 
  
class B { 
    B() {} 
}; 
  
class C:B { 
}; 
  
class D { 
    virtual void fn() {} 
}; 
  
// Driver Code 
int main() 
{ 
    cout << std::boolalpha; 
  
    // Check if int is is nothrow 
    // copy assignable or not 
    cout << "int:"
         << is_nothrow_copy_assignable<int>::value 
         << endl; 
  
    // Check if struct X is nothrow 
    // copy assignable or not 
    cout << "struct X:"
         << is_nothrow_copy_assignable<X>::value 
         << endl; 
  
    // Check if struct Y is isnothrow 
    // copy assignable or not 
    cout << "struct Y:"
         << is_nothrow_copy_assignable<Y>::value 
         << endl; 
  
    // Check if int[2] is is nothrow 
    // copy assignable or not 
    cout << "int[2]:"
         << is_nothrow_copy_assignable<int[2]>::value 
         << endl; 
  
    // Check if class A is a nothrow 
    // copy assignable or not 
    cout << "class A:"
         << is_nothrow_copy_assignable<A>::value 
         << endl; 
  
    // Check if class B is a nothrow 
    // copy assignable or not 
    cout << "class B:"
         << is_nothrow_copy_assignable<B>::value 
         << endl; 
  
    // Check if class C is a nothrow 
    // copy assignable or not 
    cout << "class C:"
         << is_nothrow_copy_assignable<C>::value 
         << endl; 
  
    // Check if class D is a nothrow 
    // copy assignable or not 
    cout << "class D:"
         << is_nothrow_copy_assignable<D>::value 
         << endl; 
    return 0; 
}
輸出:
int:true
struct X:true
struct Y:false
int[2]:false
class A:true
class B:true
class C:true
class D:true

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




相關用法


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