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


C++ std::is_trivially_copyable模板用法及代碼示例

<type_traits>頭文件中提供了C++ STL的std::is_trivially_copyable模板。 C++ STL的std::is_trivially_copyable模板用於檢查T是否為平凡可複製的類型(存儲連續的類型)。如果T是平凡可複製的類型,則它返回布爾值true,否則返回false。

頭文件:

#include<type_traits>

模板類別:

template<class T>
struct is_trivially_copyable;

用法:

std::is_trivially_copyable<T>::value

參數:模板std::is_trivially_copyable接受單個參數T(Trait類),以檢查T是否是平凡可複製的類型。



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

  • True:如果類型T是平凡可複製的。
  • False:如果類型T不是簡單可複製的。

下麵是演示C++中std::is_trivially_copyable模板的程序:

程序:

// C++ program to illustrate 
// std::is_trivially_copyable 
#include <bits/stdc++.h> 
#include <type_traits> 
using namespace std; 
  
// Declare structures 
struct X { 
    int a; 
}; 
  
struct Y { 
    Y(const Y&) {} 
}; 
  
struct Z { 
    virtual void GFG(); 
}; 
  
struct A { 
    ~A() = delete; 
}; 
  
struct B:A { 
}; 
  
// Driver Code 
int main() 
{ 
    cout << boolalpha; 
  
    // Check if X is a trivially 
    // copyable or not 
    cout << is_trivially_copyable<X>::value 
         << endl; 
  
    // Check if Y is a trivially 
    // copyable or not 
    cout << is_trivially_copyable<Y>::value 
         << endl; 
  
    // Check if Z is a trivially 
    // copyable or not 
    cout << is_trivially_copyable<Z>::value 
         << endl; 
  
    // Check if A is a trivially 
    // copyable or not 
    cout << is_trivially_copyable<A>::value 
         << endl; 
  
    // Check if B is a trivially 
    // copyable or not 
    cout << is_trivially_copyable<B>::value 
         << endl; 
  
    return 0; 
}
輸出:
true
false
false
true
true




相關用法


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