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


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

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

頭文件:

#include<type_traits>

模板類別:

template <class T>
struct is_nothrow_default_constructible;

用法:

std::is_nothrow_default_constructible::value

參數:模板std::is_nothrow_default_constructible接受單個參數T(Trait類),以檢查T是否為默認的可構造類型。



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

  • 正確:如果類型T為is_nothrow_default_constructible。
  • False:如果類型T不是is_nothrow_default_constructible。

下麵是說明C /C++中的std::is_nothrow_default_constructible模板的程序:

程序1:

// C++ program to illustrate 
// std::is_nothrow_default_constructible 
#include <bits/stdc++.h> 
#include <type_traits> 
using namespace std; 
  
// Declare structures 
struct X { 
    X(int, float){}; 
}; 
  
struct Y { 
    Y(const Y&) {} 
}; 
  
struct Z { 
    int n; 
    Z() = default; 
}; 
  
// Driver Code 
int main() 
{ 
  
    cout << boolalpha; 
  
    // Check if int is nothrow default 
    // constructible or not 
    cout << "int:"
         << is_nothrow_default_constructible<int>::value 
         << endl; 
  
    // Check if struct X is nothrow default 
    // constructible or not 
    cout << "struct X:"
         << is_nothrow_default_constructible<X>::value 
         << endl; 
  
    // Check if struct Y is nothrow default 
    // constructible or not 
    cout << "struct Y:"
         << is_nothrow_default_constructible<Y>::value 
         << endl; 
  
    // Check if struct Z is nothrow default 
    // constructible or not 
    cout << "struct Z:"
         << is_nothrow_default_constructible<Z>::value 
         << endl; 
  
    // Check if constructor X(int, float) is 
    // nothrow default constructible or not 
    cout << "constructor X(int, float):"
         << is_nothrow_default_constructible<X(int, float)>::value 
         << endl; 
    return 0; 
}
輸出:
int:true
struct X:false
struct Y:false
struct Z:true
constructor X(int, float):false

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




相關用法


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