当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ std::is_trivially_move_constructible用法及代码示例


<type_traits>头文件中提供了C++ STL的std::is_trivially_move_constructible模板。 C++ STL的std::is_trivially_move_constructible模板用于检查T是否平移构造体。如果T是平凡的可构造类型,则它返回布尔值true,否则返回false。

头文件:

#include<type_traits>

模板类别:

template< class T >
struct is_trivially_move_constructible;

用法:

std::is_trivially_move_constructible<T>::value

参数:模板std::is_trivially_move_constructible接受单个参数T(Trait类),以检查T是否是平凡的可构造类型。



返回值:模板std::is_trivially_move_constructible返回一个布尔变量,如下所示:

  • True:如果类型T是平凡的可构造的。
  • False:如果类型T不是平凡的可构造对象。

下面是演示C++中std::is_trivially_move_constructible的程序:

程序1:

// 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; 
}
输出:
int:true
class A:true
class B:true
enum class C:true
class D:true

程序2:

// 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'; 
}
输出:
Ex1 is move-constructible? true
Ex1 is trivially move-constructible? false
Ex2 is trivially move-constructible? false

参考: http://www.cplusplus.com/reference/type_traits/is_trivially_move_constructible/




相关用法


注:本文由纯净天空筛选整理自bansal_rtk_大神的英文原创作品 std::is_trivially_move_constructible in C++ with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。