std::is_trivially_constructible in C++ with Examples

The std::is_trivially_constructible template of C++ STL is present in the <type_traits> header file. The std::is_trivially_constructible template of C++ STL is used to check whether the given type T is trivially constructible type with the set of arguments or not. It return the boolean value true if T is trivially constructible type, otherwise return false.

Header File:

#include<type_traits>

Template Class:

template <class T, class.. Args>
struct is_trivially_constructible;

Syntax:

std::is_trivially_constructible::value

Parameters: The template std::is_trivially_constructible accepts two parameter:



  • T: A data type, or an array of unknown bound.
  • Args: A list of data types representing the argument types for the constructor form, in the same order as in the constructor.

Return Value: This template returns a boolean variable as shown below:

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

Below is the program to illustrates the std::is_trivially_constructible template in C/C++:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// std::is_trivially_constructible
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
  
// Declare structures
struct Ex1 {
    std::string str;
};
struct Ex2 {
    int n;
    Ex2() = default;
};
  
struct A {
  
    // Constructor
    A(int, int){};
};
  
// Driver Code
int main()
{
    cout << boolalpha;
  
    // Check if Ex1 is a trivally
    // constructible or not
    cout << "Ex1: "
         << is_trivially_constructible<Ex1>::value
         << endl;
  
    // Check if struct Ex2 is a trivally
    // constructible or not
    cout << "Ex2: "
         << is_trivially_constructible<Ex2>::value
         << endl;
  
    // Check if A(int, float) is a trivally
    // constructible or not
    cout << "A(int, int): "
         << is_trivially_constructible<A(int, int)>::value
         << endl;
    return 0;
}

chevron_right


Output:

Ex1: false
Ex2: true
A(int, int): false

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// std::is_trivially_constructible
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
  
// Declare structures
struct X {
};
  
struct Y {
  
    // Default Constructor
    Y() {}
  
    // Parameterized Constructor
    Y(const X&)
    noexcept {}
};
  
// Driver Code
int main()
{
    cout << boolalpha;
  
    // Check if int is a trivally
    // constructible or not
    cout << "int(): "
         << is_trivially_constructible<int>::value
         << endl;
  
    // Check if struct Y is a trivally
    // constructible or not
    cout << "Y(): "
         << is_trivially_constructible<Y>::value
         << endl;
  
    // Check if Y(X) is a trivally
    // constructible or not
    cout << "Y(X): "
         << is_trivially_constructible<Y, X>::value
         << endl;
  
    return 0;
}

chevron_right


Output:

int(): true
Y(): false
Y(X): false

Reference: http://www.cplusplus.com/reference/type_traits/is_trivially_constructible/

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




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.