Open In App

std::is_constructible template in C++ with Examples

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

#include<type_traits>

Template Class:



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

Syntax:

std::is_constructible::value

Parameters:



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

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




// C++ program to illustrate
// std::is_constructible example
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
 
// Declare structures
struct A {
};
 
struct T {
    T(int, int){};
};
 
// Driver Code
int main()
{
    cout << std::boolalpha;
 
    // Check if <int> is
    // constructible or not
    cout << "int: "
         << is_constructible<int>::value
         << endl;
 
    // Check if <int, float> is
    // constructible or not
    cout << "int(float): "
         << is_constructible<int, float>::value
         << endl;
 
    // Check if <int, float, float> is
    // constructible or not
    cout << "int(float, float): "
         << is_constructible<int, float, float>::value
         << endl;
 
    // Check if struct T is
    // constructible or not
    cout << "T: "
         << is_constructible<T>::value
         << endl;
 
    // Check if struct <T, int> is
    // constructible or not
    cout << "T(int): "
         << is_constructible<T, int>::value
         << endl;
 
    // Check if struct <T, int, int> is
    // constructible or not
    cout << "T(int, int): "
         << is_constructible<T, int, int>::value
         << endl;
    return 0;
}

Output:
int: true
int(float): true
int(float, float): false
T: false
T(int): false
T(int, int): true

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


Article Tags :
C++