C++ 中的 std::is_nothrow_copy_constructible 示例
C++ STL 中的 std::is_nothrow_copy_constructible 模板位于 ** 
头文件:
#include<type_traits>
模板类:
template <class T>
struct is_nothrow_copy_constructible
语法:
is_nothrow_copy_constructible<T>::value
参数: 模板 std::is_nothrow_copy_constructible 接受一个参数 T(Trait类) ,以检查 T 是否为可以复制构造的类型。
返回值: 模板 std::is_nothrow_copy_constructible 将返回布尔变量,如下所示:
- True: 如果类型 T 是可以复制构造的类型。
 - False: 如果类型 T 不是可以复制构造的类型。
 
以下是C++中演示 std::is_nothrow_copy_constructible 的程序:
程序1:
// C++ program to illustrate
// std::is_nothrow_copy_constructible
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
  
// Define Classes
class X {
};
  
class Y {
    Y(const Y&) {}
};
  
// Driver Code
int main()
{
  
    cout << boolalpha;
  
    // Check if int is no throw copy
    // constructible
    cout << "int: "
         << is_nothrow_copy_constructible<int>::value
         << endl;
  
    // Check if class X is no throw copy
    // constructible
    cout << "class X: "
         << is_nothrow_copy_constructible<X>::value
         << endl;
  
    // Check if class Y is no throw copy
    // constructible
    cout << "class Y: "
         << is_nothrow_copy_constructible<Y>::value
         << endl;
  
    return 0;
}
int: true
class X: true
class Y: false
程序2:
// C++ program to illustrate
// std::is_nothrow_copy_constructible
#include <iostream>
#include <type_traits>
using namespace std;
  
// Declare structures
struct A {
};
  
struct B {
    B(const B&) {}
};
  
struct C {
    C(const C&)
    noexcept {}
};
  
// Driver Code
int main()
{
  
    cout << boolalpha;
    cout << "is_nothrow_copy_constructible:"
         << endl;
  
    cout << "int is_nothrow_copy_constructible? "
         << is_nothrow_copy_constructible<int>::value
         << endl;
  
    cout << "A is_nothrow_copy_constructible? "
         << is_nothrow_copy_constructible<A>::value
         << endl;
  
    cout << "B is_nothrow_copy_constructible? "
         << is_nothrow_copy_constructible<B>::value
         << endl;
  
    cout << "C is_nothrow_copy_constructible? "
         << is_nothrow_copy_constructible<C>::value
         << endl;
  
    return 0;
}
is_nothrow_copy_constructible:
int is_nothrow_copy_constructible? true
A is_nothrow_copy_constructible? true
B is_nothrow_copy_constructible? false
C is_nothrow_copy_constructible? true
参考资料: http://www.cplusplus.com/reference/type_traits/is_nothrow_copy_constructible/
极客教程