La plantilla std::is_trivially_assignable de C++ STL está presente en el archivo de encabezado <type_traits>. La plantilla std::is_trivally_assignable de C++ STL se utiliza para comprobar si un valor del tipo B se puede asignar al tipo A o no. Devuelve el valor booleano ya sea verdadero o falso.
Archivo de cabecera:
#include<type_traits>
Clase de plantilla:
template <class A, Class B> struct is_trivially_assignable;
Sintaxis:
std::is_trivially_assignable::value
Parámetros:
- R: Representa el tipo de objeto que recibe la asignación.
- B: Representa el tipo de objeto que proporciona el valor.
Valor devuelto: la plantilla std::is_trivially_assignable devuelve una variable booleana como se muestra a continuación:
- Verdadero: si el tipo A es asignable al tipo B.
- Falso: si el tipo A no es asignable al tipo B.
A continuación se muestra el programa para demostrar std::is_trivially_assignable en C++ :
Programa 1:
// C++ program to illustrate // std::is_trivially_assignable #include <bits/stdc++.h> #include <type_traits> 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<int&, double>::value << endl; // Check if struct A is assignable to // struct A or not cout << "is struct A = struct A? " << is_trivially_assignable<A, A>::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
Programa 2:
// C++ program to illustrate // std::is_trivially_assignable #include <bits/stdc++.h> #include <type_traits> 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<GfG, gfg>::value << endl; // Check if class GfG is assignable to // class GfG or not cout << "is class GfG = class GfG? " << is_trivially_assignable<GfG, GfG>::value << endl; // Check if enum class AB is assignable // from class gfg or not cout << "is class AB = class gfg? " << is_trivially_assignable<AB, gfg>::value << endl; // Check if class GfG assignable to // class gfg or not cout << "is class Gfg = class gfg? " << is_trivially_assignable<GfG, gfg>::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
Referencia: http://www.cplusplus.com/reference/type_traits/is_trivially_assignable/
Publicación traducida automáticamente
Artículo escrito por bansal_rtk_ y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA