std::is_trivially_move_constructible in C++ with Examples

The std::is_trivially_move_constructible template of C++ STL is present in the <type_traits> header file. The std::is_trivially_move_constructible template of C++ STL is used to check whether the T is trivially move constructibe or not. It return the boolean value true if T is trivially move constructible type, Otherwise return false.

Header File:

#include<type_traits>

Template Class:

template< class T >
struct is_trivially_move_constructible;

Syntax:

std::is_trivially_move_constructible<T>::value

Parameter: The template std::is_trivially_move_constructible accepts a single parameter T(Trait class) to check whether T is trivially move constructible type or not.



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

  • True: If the type T is a trivially move constructible.
  • False: If the type T is not a trivially move constructible.

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

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate
// std::is_trivially_move_constructible
#include <iostream>
#include <type_traits>
using namespace std;
  
// Declaration of classes
class A {
};
  
class B {
    B() {}
};
  
enum class C : int { x,
                     y,
                     z };
  
class D {
    int v1;
    double v2;
  
public:
    D(int n)
        : v1(n), v2()
    {
    }
    D(int n, double f)
    noexcept : v1(n), v2(f) {}
};
  
int main()
{
    cout << boolalpha;
  
    // Check if int is trivially
    // move constructible or not
    cout << "int: "
         << is_trivially_move_constructible<int>::value
         << endl;
  
    // Check if class A is trivially
    // move constructible or not
    cout << "class A: "
         << is_trivially_move_constructible<A>::value
         << endl;
  
    // Check if class B is trivially
    // move constructible or not
    cout << "class B: "
         << is_trivially_move_constructible<B>::value
         << endl;
  
    // Check if enum class C is trivially
    // move constructible or not
    cout << "enum class C: "
         << is_trivially_move_constructible<C>::value
         << endl;
  
    // Check if class D is trivially
    // move constructible or not
    std::cout << "class D: "
              << is_trivially_move_constructible<D>::value
              << endl;
    return 0;
}

chevron_right


Output:

int: true
class A: true
class B: true
enum class C: true
class D: true

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate
// std::is_trivially_move_constructible
#include <iostream>
#include <type_traits>
using namespace std;
  
// Declare structures
struct Ex1 {
    Ex1() {}
    Ex1(Ex1&&)
    {
        cout << "Throwing move constructor!";
    }
    Ex1(const Ex1&)
    {
        cout << "Throwing copy constructor!";
    }
};
  
struct Ex2 {
    Ex2() {}
    Ex2(Ex2&&) noexcept
    {
        cout << "Non-throwing move constructor!";
    }
    Ex2(const Ex2&) noexcept
    {
        cout << "Non-throwing copy constructor!";
    }
};
  
// Driver Code
int main()
{
    cout << boolalpha;
  
    // Check if struct Ex1 is move
    // constructible or not
    cout << "Ex1 is move-constructible? "
         << is_move_constructible<Ex1>::value
         << '\n';
  
    // Check if struct Ex1 is trivially
    // move constructible or not
    cout << "Ex1 is trivially move-constructible? "
         << is_trivially_move_constructible<Ex1>::value
         << '\n';
  
    // Check if struct Ex2 is trivially
    // move constructible or not
    cout << "Ex2 is trivially move-constructible? "
         << is_trivially_move_constructible<Ex2>::value
         << '\n';
}

chevron_right


Output:

Ex1 is move-constructible? true
Ex1 is trivially move-constructible? false
Ex2 is trivially move-constructible? false

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