C++中带有示例的std::is_trivially_copyable模板
C ++ STL的 std::is_trivially_copyable 模板存在于 <type_traits> 头文件中。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(特质类) 以检查 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
极客教程