Open In App

std::is_trivially_copy_constructible in C/C++

Last Updated : 30 Mar, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
The std::is_trivially_copy_constructible template is a type that can be trivially constructed from a value or reference of the same type. This includes scalar types, trivially copy constructible classes and arrays of such types. This algorithm is about to test whether a type is trivially copied constructible or not. It returns the boolean value showing the same. Header File:
#include <type_traits>
Template Class:
template <class T> 
struct is_trivially_copy_constructible;
If T is_trivially_copy_constructible, then it inherits from true_type else inherits from false_type. Syntax:
std::is_trivially_copy_constructible<int>::value
std::is_trivially_copy_constructible<class T>::value
Parameter: This template accepts a single parameter T(Trait class) to check if T is trivially copy constructible or not. Return Value: This template return a boolean variable as shown below:
  • True: If the type is trivially copy constructible.
  • False: If the type is not trivially copy constructible.
Below programs illustrates the std::is_trivially_copy_constructible template in C/C++: Program 1: CPP
// C++ program to illustrate
// is_trivially_copy_constructible
#include <iostream>
#include <type_traits>
using namespace std;

// Struct Class
struct A {
};
struct B {
    B(const B&) {}
};
struct C {
    virtual void fn() {}
};

// Driver Code
int main()
{
    cout << boolalpha;
    cout << "is_trivially_copy_constructible: "
         << endl;
    cout << "int: " << is_trivially_copy_constructible<int>::value
         << endl;
    cout << "A: " << is_trivially_copy_constructible<A>::value
         << endl;
    cout << "B: " << is_trivially_copy_constructible<B>::value
         << endl;
    cout << "C: " << is_trivially_copy_constructible<C>::value
         << endl;
    return 0;
}
Output:
is_trivially_copy_constructible: 
int: true
A: true
B: false
C: false
Program 2: CPP
// C++ program to illustrate
// is_trivially_copy_constructible
#include <iostream>
#include <type_traits>
using namespace std;

// Structure
struct Ex1 {
    string str;
};
struct Ex2 {
    int n;
    Ex2(const Ex2&) = default;
};

// Driver Code
int main()
{
    cout << boolalpha;
    cout << "Ex1 is copy-constructible? ";

    cout << is_copy_constructible<Ex1>::value
         << endl;

    cout << "Ex1 is trivially copy-constructible? ";

    cout << is_trivially_copy_constructible<Ex1>::value
         << endl;

    cout << "Ex2 is trivially copy-constructible? ";

    cout << is_trivially_copy_constructible<Ex2>::value
         << endl;

    cout << "Ex2 is nothrow copy-constructible? ";

    cout << is_nothrow_copy_constructible<Ex2>::value
         << endl;
}
Output:
Ex1 is copy-constructible? true
Ex1 is trivially copy-constructible? false
Ex2 is trivially copy-constructible? true
Ex2 is nothrow copy-constructible? true

Article Tags :
Practice Tags :

Similar Reads