📌  相关文章
📜  C++中的std :: is_assignable模板及其示例

📅  最后修改于: 2021-05-30 16:44:27             🧑  作者: Mango

< type_traits >头文件中提供了C++ STL的std :: is_assignable模板。 C++ STL的std :: is_assignable模板用于检查是否可以将B类型的值分配给A。它返回布尔值true或false。以下是相同的语法:

头文件:

#include

句法:

template 
   
  struct is_assignable;;

参数:它接受以下参数:

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

返回值:模板std :: is_assignable():返回一个布尔值,即true或false。

以下是演示std :: is_assignable()的程序:

程序1:

// C++ program to illustrate
// is_assignable example
  
#include 
#include 
  
using namespace std;
  
struct A {
};
struct B {
    B& operator=(const A&)
    {
        return *this;
    }
};
  
// Driver Code
int main()
{
    cout << boolalpha;
    cout << "is_assignable:" << endl;
    cout << "A = B: "
         << is_assignable::value
         << endl;
    cout << "B = A: "
         << is_assignable::value
         << endl;
  
    return 0;
}
输出:
is_assignable:
A = B: false
B = A: true

程式2:

// C++ program to illustrate
// is_assignable example
  
#include 
  
#include 
  
using namespace std;
  
struct B {
};
struct A {
    A& operator=(const B&)
    {
        return *this;
    }
};
  
// Driver Code
int main()
{
    cout << boolalpha;
    cout << "is_assignable:" << endl;
    cout << "A = B: "
         << is_assignable::value
         << endl;
    cout << "B = A: "
         << is_assignable::value
         << endl;
  
    return 0;
}
输出:
is_assignable:
A = B: true
B = A: false

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

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