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

📅  最后修改于: 2021-05-30 04:17:22             🧑  作者: Mango

头文件中提供了C++ STL的std :: is_trivially_assignable模板。 C++ STL的std :: is_trivally_assignable模板用于检查是否可以将类型B的值分配给类型A。它返回布尔值true或false。

头文件:

#include

模板类别:

template 
struct is_trivially_assignable;

句法:

std::is_trivially_assignable::value

参数:

  • 答:它代表接收分配的对象的类型。
  • B:代表提供值的对象的类型。

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

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

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

程序1:

// C++ program to illustrate
// std::is_trivially_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;
    }
};
  
// Driver Code
int main()
{
  
    cout << boolalpha;
  
    // Check if int& is assignable to
    // double or not
    cout << "is int = double? "
         << is_trivially_assignable::value
         << endl;
  
    // Check if struct A is assignable to
    // struct A or not
    cout << "is struct A = struct A? "
         << is_trivially_assignable::value
         << endl;
  
    return 0;
}
输出:
is int = double? true
is struct A = struct A? true
is class B = class A? false
is class B = class B? false
is class AB = class gfg? false
is class Gfg = class gfg? false

程式2:

// C++ program to illustrate
// std::is_trivially_assignable
#include 
#include 
using namespace std;
  
// Declare classes
class GfG {
};
  
class gfg {
};
  
enum class AB : int { x,
                      y,
                      z };
  
// Driver Code
int main()
{
  
    cout << boolalpha;
  
    // Check if class GfG is assignable to
    // class gfg or not
    cout << "is class GfG = class gfg? "
         << is_trivially_assignable::value
         << endl;
  
    // Check if class GfG is assignable to
    // class GfG or not
    cout << "is class GfG = class GfG? "
         << is_trivially_assignable::value
         << endl;
  
    // Check if enum class AB is assignable
    // from class gfg or not
    cout << "is class AB = class gfg? "
         << is_trivially_assignable::value
         << endl;
  
    // Check if class GfG assignable to
    // class gfg or not
    cout << "is class Gfg = class gfg? "
         << is_trivially_assignable::value
         << endl;
    return 0;
}
输出:
is class GfG = class gfg? false
is class GfG = class GfG? true
is class AB = class gfg? false
is class Gfg = class gfg? false

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

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