std::is_nothrow_default_constructible in C++ with Examples

The std::is_nothrow_default_constructible template of C++ STL is present in the <type_traits> header file. The std::is_nothrow_default_constructible template of C++ STL is used to check whether the given type T is default constructible type or not and this is known for not to throw any exception. 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_nothrow_default_constructible;

Syntax:

std::is_nothrow_default_constructible::value

Parameters: The template std::is_nothrow_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 a is_nothrow_default_constructible.
  • False: If the type T is not a is_nothrow_default_constructible.

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

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// std::is_nothrow_default_constructible
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
  
// Declare structures
struct X {
    X(int, float){};
};
  
struct Y {
    Y(const Y&) {}
};
  
struct Z {
    int n;
    Z() = default;
};
  
// Driver Code
int main()
{
  
    cout << boolalpha;
  
    // Check if int is nothrow default
    // constructible or not
    cout << "int: "
         << is_nothrow_default_constructible<int>::value
         << endl;
  
    // Check if struct X is nothrow default
    // constructible or not
    cout << "struct X: "
         << is_nothrow_default_constructible<X>::value
         << endl;
  
    // Check if struct Y is nothrow default
    // constructible or not
    cout << "struct Y: "
         << is_nothrow_default_constructible<Y>::value
         << endl;
  
    // Check if struct Z is nothrow default
    // constructible or not
    cout << "struct Z: "
         << is_nothrow_default_constructible<Z>::value
         << endl;
  
    // Check if constructor X(int, float) is
    // nothrow default constructible or not
    cout << "constructor X(int, float): "
         << is_nothrow_default_constructible<X(int, float)>::value
         << endl;
    return 0;
}

chevron_right


Output:

int: true
struct X: false
struct Y: false
struct Z: true
constructor X(int, float): false

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