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


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


<type_traits>頭文件中提供了C++ STL的std::is_trivially_default_constructible模板。 C++ STL的std::is_trivially_default_constructible模板用於檢查T是否是普通的默認可構造類型。如果T是平凡的默認可構造類型,則它返回布爾值true,否則返回false。

頭文件:

#include<type_traits>

模板類別:

template< class T >
struct is_default_constructible;

用法:

std::is_trivially_default_constructible<T>::value

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



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

  • True:如果類型T是瑣碎的默認可構造對象。
  • False:如果類型T不是簡單的默認可構造對象。

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

程序1:

// C++ program to demonstrate 
// std::is_trivially_default_constructible 
#include <iostream> 
#include <type_traits> 
using namespace std; 
  
// Declaration of classes 
class A { 
}; 
  
class B { 
    B() {} 
}; 
  
enum class C:int { x, 
                     y, 
                     z }; 
  
class D { 
    int v1; 
    double v2; 
  
public:
    D(int n) 
        :v1(n), v2() 
    { 
    } 
    D(int n, double f) 
    noexcept:v1(n), v2(f) {} 
}; 
  
int main() 
{ 
    cout << boolalpha; 
  
    // Check if int is trivially 
    // default constructible or not 
    cout << "int:"
         << is_trivially_default_constructible<int>::value 
         << endl; 
  
    // Check if class A is trivially 
    // default constructible or not 
    cout << "class A:"
         << is_trivially_default_constructible<A>::value 
         << endl; 
  
    // Check if class B is trivially 
    // default constructible or not 
    cout << "class B:"
         << is_trivially_default_constructible<B>::value 
         << endl; 
  
    // Check if enum class C is trivially 
    // default constructible or not 
    cout << "enum class C:"
         << is_trivially_default_constructible<C>::value 
         << endl; 
  
    // Check if class D is trivially 
    // default constructible or not 
    std::cout << "class D:"
              << is_trivially_default_constructible<D>::value 
              << endl; 
    return 0; 
}
輸出:
int:true
class A:true
class B:false
enum class C:true
class D:false

程序2:

// C++ program to demonstrate 
// std::is_trivially_default_constructible 
#include <iostream> 
#include <type_traits> 
using namespace std; 
  
// Declare structures 
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 struct Ex1 is default 
    // constructible or not 
    cout << "Ex1 is default-constructible? "
         << is_trivially_default_constructible<Ex1>::value 
         << '\n'; 
  
    // Check if struct Ex1 is trivially 
    // defaultconstructible or not 
    cout << "Ex1 is trivially default-constructible? "
         << is_trivially_default_constructible<Ex1>::value 
         << '\n'; 
  
    // Check if struct Ex2 is trivially 
    // defaultconstructible or not 
    cout << "Ex2 is trivially default-constructible? "
         << is_trivially_default_constructible<Ex2>::value 
         << '\n'; 
}
輸出:
Ex1 is default-constructible? false
Ex1 is trivially default-constructible? false
Ex2 is trivially default-constructible? false

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




相關用法


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