std::is_trivially_copy_constructible in C/C++

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

is_trivially_copy_constructible: 
int: true
A: true
B: false
C: false

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

Ex1 is copy-constructible? true
Ex1 is trivially copy-constructible? false
Ex2 is trivially copy-constructible? true
Ex2 is nothrow copy-constructible? true

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.