std::is_default_constructible in C++ with Examples

The std::is_default_constructible template of C++ STL is present in the <type_traits> header file. The std::is_default_constructible template of C++ STL is used to check whether the T is default constructible or not. A default constructible can be constructed without arguments or initialization values. It return the boolean value true if T is default constructible type, Otherwise return false.

Header File:

#include<type_traits>

Template Class:

template <class T> 
struct is_default_constructible;

Syntax:

std::is_default_constructible>class T> ::value

Parameters: The template std::is_default_constructible accepts a single parameter T(Trait class) to check whether T is default constructible type or not.



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

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

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

Program:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// std::is_default_constructible
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
  
// Declare structures
struct A {
};
  
struct B {
    int n;
    B() = default;
};
  
// Class
class classA {
    ~classA() = delete;
};
  
// Inherited class
class classB : classA {
};
  
// Driver Code
int main()
{
    cout << boolalpha;
  
    // Check if int is default constructible?
    cout << "int : "
         << is_default_constructible<int>::value
         << endl;
  
    // Check if struct A is default constructible?
    cout << "struct A: "
         << is_default_constructible<A>::value
         << endl;
  
    // Check if struct B is default constructible?
    cout << "struct B: "
         << is_default_constructible<B>::value
         << endl;
  
    // Check if classA is default constructible?
    cout << "classA: "
         << is_default_constructible<classA>::value
         << endl;
  
    // Check if classB is default constructible?
    cout << "classB: "
         << is_default_constructible<classB>::value
         << endl;
  
    return 0;
}

chevron_right


Output:

int : true
struct A: true
struct B: true
classA: false
classB: false

Reference: http://www.cplusplus.com/reference/type_traits/is_default_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.