Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

std::is_trivially_copyable template in C++ with Examples

  • Last Updated : 12 Jun, 2020

The std::is_trivially_copyable template of C++ STL is present in the <type_traits> header file. The std::is_trivially_copyable template of C++ STL is used to check whether T is trivially copyable type (a type whose storage is contiguous) or not. It return the boolean value true if T is trivially copyable type, otherwise return false.

Header File:

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
#include<type_traits>

Template Class:

template<class T>
struct is_trivially_copyable;

Syntax:



std::is_trivially_copyable<T>::value

Parameter: The template std::is_trivially_copyable accepts a single parameter T(Trait class) to check whether T is trivially copyable type or not.

Return Value:The template std::is_trivially_copyable returns a boolean variable as shown below:

  • True: If the type T is a trivially copyable.
  • False: If the type T is not a trivially copyable.

Below is the program to demonstrate std::is_trivially_copyable template in C++:

Program:




// 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;
}
Output:
true
false
false
true
true



My Personal Notes arrow_drop_up
Recommended Articles
Page :