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


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

<type_traits>头文件中提供了C++ STL的std::is_nothrow_copy_constructible模板。 C++ STL的std::is_nothrow_copy_constructible模板用于检查T是否为可复制构造类型,并且众所周知它不会引发任何异常。如果T是可复制构造类型,则返回布尔值true,否则返回false。

头文件:

#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/




相关用法


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