Open In App

std::is_trivially_assignable in C++ with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

The std::is_trivially_assignable template of C++ STL is present in the <type_traits> header file. The std::is_trivally_assignable template of C++ STL is used to check whether a value of type B can be assigned to type A or not. It returns the boolean value either true or false.

Header File:

#include<type_traits>

Template Class:

template <class A, Class B>
struct is_trivially_assignable;

Syntax:

std::is_trivially_assignable::value

Parameters:

  • A: It represent the type of object that receives the assignment.
  • B: It represent the type of the object that provides the value.

Return Value: The template std::is_trivially_assignable returns a boolean variable as shown below:

  • True: If the type A is assignable to type B.
  • False: If the type A is not assignable to type B.

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

Program 1:




// C++ program to illustrate
// std::is_trivially_assignable
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
  
// Declare structures
struct A {
};
  
struct B {
    B& operator=(const A&) noexcept
    {
        return *this;
    }
    B& operator=(const B&)
    {
        return *this;
    }
};
  
// Driver Code
int main()
{
  
    cout << boolalpha;
  
    // Check if int& is assignable to
    // double or not
    cout << "is int = double? "
         << is_trivially_assignable<int&,
                                    double>::value
         << endl;
  
    // Check if struct A is assignable to
    // struct A or not
    cout << "is struct A = struct A? "
         << is_trivially_assignable<A, A>::value
         << endl;
  
    return 0;
}


Output:

is int = double? true
is struct A = struct A? true
is class B = class A? false
is class B = class B? false
is class AB = class gfg? false
is class Gfg = class gfg? false

Program 2:




// C++ program to illustrate
// std::is_trivially_assignable
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
  
// Declare classes
class GfG {
};
  
class gfg {
};
  
enum class AB : int { x,
                      y,
                      z };
  
// Driver Code
int main()
{
  
    cout << boolalpha;
  
    // Check if class GfG is assignable to
    // class gfg or not
    cout << "is class GfG = class gfg? "
         << is_trivially_assignable<GfG, gfg>::value
         << endl;
  
    // Check if class GfG is assignable to
    // class GfG or not
    cout << "is class GfG = class GfG? "
         << is_trivially_assignable<GfG, GfG>::value
         << endl;
  
    // Check if enum class AB is assignable
    // from class gfg or not
    cout << "is class AB = class gfg? "
         << is_trivially_assignable<AB, gfg>::value
         << endl;
  
    // Check if class GfG assignable to
    // class gfg or not
    cout << "is class Gfg = class gfg? "
         << is_trivially_assignable<GfG, gfg>::value
         << endl;
    return 0;
}


Output:

is class GfG = class gfg? false
is class GfG = class GfG? true
is class AB = class gfg? false
is class Gfg = class gfg? false

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



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