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

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

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

头文件:

#include

模板类别:

template< class A, class B >
struct is_nothrow_assignable;

句法:

std::is_assignable::value

参数:模板std :: is_assignable接受以下参数:

  • A:它代表参数B被隐式分配的参数。
  • B:表示可分配给A的参数类型。

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

  • 正确:如果类型A可分配给类型B。
  • False:如果类型A不可分配给类型B。

下面是演示C++中std :: is_nothrow_assignable模板的程序:

程序:

// C++ program to illustrate
// std::is_nothrow_assignable
#include 
#include 
using namespace std;
  
// Declare structures
struct A {
};
  
struct B {
    B& operator=(const A&) noexcept
    {
        return *this;
    }
    B& operator=(const B&)
    {
        return *this;
    }
};
  
// Declare classes
class GfG {
};
  
class gfg {
};
  
enum class AB : int { x,
                      y,
                      z };
  
// Driver Code
int main()
{
  
    cout << boolalpha;
  
    // Check if int& is assignable to
    // double or not
    cout << "is int = double? "
         << is_nothrow_assignable::value
         << endl;
  
    // Check if struct A is assignable to
    // srtuct A or not
    cout << "is struct A = struct A? "
         << is_nothrow_assignable::value
         << endl;
  
    // Check if struct B is assignable to
    // srtuct A or not
    cout << "is class B = class A? "
         << is_nothrow_assignable::value
         << endl;
  
    // Check if struct B is assignable to
    // srtuct B or not
    cout << "is class B = class B? "
         << is_nothrow_assignable::value
         << endl;
  
    // Check if enum class AB is assignable
    // from class gfg or not
    cout << "is class AB = class gfg? "
         << is_nothrow_assignable::value
         << endl;
  
    // Check if class GfG assignable to
    // class gfg or not
    cout << "is class Gfg = class gfg? "
         << is_nothrow_assignable::value
         << endl;
    return 0;
}
输出:
is int = double? true
is struct A = struct A? true
is class B = class A? true
is class B = class B? false
is class AB = class gfg? false
is class Gfg = class gfg? false

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

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