Open In App

std::is_copy_assignable in C++ with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

The std::is_copy_assignable template of C++ STL is present in the <type_traits> header file. The std::is_copy_assignable template of C++ STL is used to check whether the T is copy assignable or not. It return the boolean value true if T is copy assignable type, otherwise return false.

Header File:

#include<type_traits>

Template Class:

template <class T>
struct is_copy_assignable;

Syntax:

std::is_copy_assignable >class T> ::value

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

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

  • True: If the type T is copy assignable type.
  • False: If the type T is not a copy assignable type.

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

Program:




// C++ program to illustrate
// std::is_copy_assignable example
#include <bits/stdc++.h>
#include <type_traits>
  
using namespace std;
  
// Declare structures
struct A {
};
  
struct B {
    B& operator=(const B&) = delete;
};
  
struct C {
    C(C&&) {}
};
  
// Driver Code
int main()
{
  
    cout << boolalpha;
  
    // Check if char is_copy_assignable?
    cout << "char: "
         << is_copy_assignable<char>::value
         << endl;
  
    // Check if struct A is_copy_assignable?
    cout << "struct A: "
         << is_copy_assignable<A>::value
         << endl;
  
    // Check if struct B is_copy_assignable?
    cout << "struct B: "
         << is_copy_assignable<B>::value
         << endl;
  
    // Check if struct C is_copy_assignable?
    cout << "struct C: "
         << is_copy_assignable<C>::value
         << endl;
  
    // Check if int[2] is_copy_assignable?
    cout << "int[2]: "
         << is_copy_assignable<int[2]>::value
         << endl;
  
    return 0;
}


Output:

char: true
struct A: true
struct B: false
struct C: false
int[2]: false

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



Last Updated : 12 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads