std::is_default_constructible in C++ with Examples
Last Updated :
12 Jun, 2020
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:
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
struct A {
};
struct B {
int n;
B() = default ;
};
class classA {
~classA() = delete ;
};
class classB : classA {
};
int main()
{
cout << boolalpha;
cout << "int : "
<< is_default_constructible< int >::value
<< endl;
cout << "struct A: "
<< is_default_constructible<A>::value
<< endl;
cout << "struct B: "
<< is_default_constructible<B>::value
<< endl;
cout << "classA: "
<< is_default_constructible<classA>::value
<< endl;
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
Share your thoughts in the comments
Please Login to comment...