📌  相关文章
📜  C++中带有示例的std :: is_nothrow_copy_assignable

📅  最后修改于: 2021-05-30 09:51:47             🧑  作者: Mango

C++ STL的std :: is_nothrow_copy_assignable模板位于< type_traits >头文件中。 C++ STL的std :: is_nothrow_copy_assignable模板用于检查T是否为副本可分配类型,并且众所周知它不会引发任何异常。它返回布尔值true或false。

头文件:

#include

模板类别:

template< class T >
struct is_nothrow_copy_assignable;

句法:

std::is_nothrow_copy_assignable< datatype >::value << '\n'

参数:模板std :: is_nothrow_copy_assignable接受单个参数T (特质类),以检查T是否为is_nothrow_copy_assignable类型。

返回值:

  • 正确:如果给定的数据类型T是可分配的Nohrow副本。
  • False:如果给定的数据类型T不是可分配的Nohrow副本。

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

程序:

// C++ program to illustrate
// std::is_nothrow_copy_assignable
#include 
#include 
using namespace std;
  
// Declare structures X and Y
struct X {
};
  
struct Y {
    Y& operator=(const Y&)
    {
        return *this;
    }
};
  
// Declare classes
class A {
};
  
class B {
    B() {}
};
  
class C : B {
};
  
class D {
    virtual void fn() {}
};
  
// Driver Code
int main()
{
    cout << std::boolalpha;
  
    // Check if int is is nothrow
    // copy assignable or not
    cout << "int: "
         << is_nothrow_copy_assignable::value
         << endl;
  
    // Check if struct X is nothrow
    // copy assignable or not
    cout << "struct X: "
         << is_nothrow_copy_assignable::value
         << endl;
  
    // Check if struct Y is isnothrow
    // copy assignable or not
    cout << "struct Y: "
         << is_nothrow_copy_assignable::value
         << endl;
  
    // Check if int[2] is is nothrow
    // copy assignable or not
    cout << "int[2]: "
         << is_nothrow_copy_assignable::value
         << endl;
  
    // Check if class A is a nothrow
    // copy assignable or not
    cout << "class A: "
         << is_nothrow_copy_assignable::value
         << endl;
  
    // Check if class B is a nothrow
    // copy assignable or not
    cout << "class B: "
         << is_nothrow_copy_assignable::value
         << endl;
  
    // Check if class C is a nothrow
    // copy assignable or not
    cout << "class C: "
         << is_nothrow_copy_assignable::value
         << endl;
  
    // Check if class D is a nothrow
    // copy assignable or not
    cout << "class D: "
         << is_nothrow_copy_assignable::value
         << endl;
    return 0;
}
输出:
int: true
struct X: true
struct Y: false
int[2]: false
class A: true
class B: true
class C: true
class D: true

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

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”