Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

std::is_nothrow_copy_assignable in C++ with Examples

  • Last Updated : 12 Jun, 2020

The std::is_nothrow_copy_assignable template of C++ STL is present in the <type_traits> header file. The std::is_nothrow_copy_assignable template of C++ STL is used to check whether T is copy assignable type or not and this is known for not to throw any exception. It returns the boolean value either true or false.

Header File:

#include<type_traits>

Template Class:

template< class T >
struct is_nothrow_copy_assignable;

Syntax:

std::is_nothrow_copy_assignable< datatype >::value << '\n'

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

Return Value:

  • True: If a given data type T is a nothrow copy assignable.
  • False: If a given data type T is not a nothrow copy assignable.

Below is the program to demonstrate std::is_nothrow_copy_assignable in C++:

Program:




// C++ program to illustrate
// std::is_nothrow_copy_assignable
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
  
// Declare structures X and Y
struct X {
};
  
struct Y {
    Y& operator=(const Y&)
    {
        return *this;
    }
};
  
// Declare classes
class A {
};
  
class B {
    B() {}
};
  
class C : B {
};
  
class D {
    virtual void fn() {}
};
  
// Driver Code
int main()
{
    cout << std::boolalpha;
  
    // Check if int is is nothrow
    // copy assignable or not
    cout << "int: "
         << is_nothrow_copy_assignable<int>::value
         << endl;
  
    // Check if struct X is nothrow
    // copy assignable or not
    cout << "struct X: "
         << is_nothrow_copy_assignable<X>::value
         << endl;
  
    // Check if struct Y is isnothrow
    // copy assignable or not
    cout << "struct Y: "
         << is_nothrow_copy_assignable<Y>::value
         << endl;
  
    // Check if int[2] is is nothrow
    // copy assignable or not
    cout << "int[2]: "
         << is_nothrow_copy_assignable<int[2]>::value
         << endl;
  
    // Check if class A is a nothrow
    // copy assignable or not
    cout << "class A: "
         << is_nothrow_copy_assignable<A>::value
         << endl;
  
    // Check if class B is a nothrow
    // copy assignable or not
    cout << "class B: "
         << is_nothrow_copy_assignable<B>::value
         << endl;
  
    // Check if class C is a nothrow
    // copy assignable or not
    cout << "class C: "
         << is_nothrow_copy_assignable<C>::value
         << endl;
  
    // Check if class D is a nothrow
    // copy assignable or not
    cout << "class D: "
         << is_nothrow_copy_assignable<D>::value
         << endl;
    return 0;
}
Output:
int: true
struct X: true
struct Y: false
int[2]: false
class A: true
class B: true
class C: true
class D: true

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!