📌  相关文章
📜  C / C++中的std :: is_trivially_copy_constructible

📅  最后修改于: 2021-05-30 16:59:45             🧑  作者: Mango

std :: is_trivially_copy_constructible模板是一种类型,可以从相同类型的值或引用中进行构造。这包括标量类型,琐碎地复制可构造的类以及此类类型的数组。该算法将测试类型是否可被普通复制。它返回显示相同的布尔值。

头文件:

#include 

模板类别:

template  
struct is_trivially_copy_constructible;

如果T is_trivially_copy_constructible,则它从true_type继承,否则从false_type继承。

句法:

std::is_trivially_copy_constructible::value
std::is_trivially_copy_constructible::value

参数:该模板接受单个参数T(Trait类),以检查T是否可以普通复制构造。

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

  • 正确:如果类型是可复制构造的。
  • False:如果该类型不是平凡的副本可构造。

下面的程序说明了C / C++中的std :: is_trivially_copy_constructible模板:

程序1:

// C++ program to illustrate
// is_trivially_copy_constructible
#include 
#include 
using namespace std;
  
// Struct Class
struct A {
};
struct B {
    B(const B&) {}
};
struct C {
    virtual void fn() {}
};
  
// Driver Code
int main()
{
    cout << boolalpha;
    cout << "is_trivially_copy_constructible: "
         << endl;
    cout << "int: " << is_trivially_copy_constructible::value
         << endl;
    cout << "A: " << is_trivially_copy_constructible::value
         << endl;
    cout << "B: " << is_trivially_copy_constructible::value
         << endl;
    cout << "C: " << is_trivially_copy_constructible::value
         << endl;
    return 0;
}
输出:
is_trivially_copy_constructible: 
int: true
A: true
B: false
C: false

程式2:

// C++ program to illustrate
// is_trivially_copy_constructible
#include 
#include 
using namespace std;
  
// Structure
struct Ex1 {
    string str;
};
struct Ex2 {
    int n;
    Ex2(const Ex2&) = default;
};
  
// Driver Code
int main()
{
    cout << boolalpha;
    cout << "Ex1 is copy-constructible? ";
  
    cout << is_copy_constructible::value
         << endl;
  
    cout << "Ex1 is trivially copy-constructible? ";
  
    cout << is_trivially_copy_constructible::value
         << endl;
  
    cout << "Ex2 is trivially copy-constructible? ";
  
    cout << is_trivially_copy_constructible::value
         << endl;
  
    cout << "Ex2 is nothrow copy-constructible? ";
  
    cout << is_nothrow_copy_constructible::value
         << endl;
}
输出:
Ex1 is copy-constructible? true
Ex1 is trivially copy-constructible? false
Ex2 is trivially copy-constructible? true
Ex2 is nothrow copy-constructible? true
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”