Open In App

std::is_default_constructible in C++ with Examples

Last Updated : 12 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:




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


Output:

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

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads