std::is_default_constructible, std::is_trivially_default_constructible, std::is_nothrow_default_constructible
Definido en el archivo de encabezado <type_traits>
|
||
template< class T > struct is_default_constructible; |
(1) | (desde C++11) |
template< class T > struct is_trivially_default_constructible; |
(2) | (desde C++11) |
template< class T > struct is_nothrow_default_constructible; |
(3) | (desde C++11) |
value
igual a true, de lo contrario value
es false.value
igual a true, de lo contrario value
es false.value
igual a true, de lo contrario value
es false.T
deberá ser un tipo completo, (posiblemente calificado-cv) void, o un array de límite desconocido. De lo contrario, el comportamiento está indefinido.
Si la instanciación de una plantilla anterior depende, directa o indirectamente, de un tipo incompleto, y esa instanciación podría generar un resultado distinto si ese tipo hipotéticamente se completara, el comportamiento está indefinido.
El comportamiento de un programa que añade especializaciones para cualquiera de las plantillas definidas en esta página no está definido.
Contenido |
[editar] Plantillas de variable auxiliares
template< class T > inline constexpr bool is_default_constructible_v = is_default_constructible<T>::value; |
(desde C++17) | |
template< class T > inline constexpr bool is_trivially_default_constructible_v = is_trivially_default_constructible<T>::value; |
(desde C++17) | |
template< class T > inline constexpr bool is_nothrow_default_constructible_v = is_nothrow_default_constructible<T>::value; |
(desde C++17) | |
Heredado de std::integral_constant
Constantes miembro
value [estático] |
true si T puede construirse por defecto , de lo contrario false. (constante miembro pública estática) |
Funciones miembro
operator bool |
Convierte el objeto a bool, devuelve value . (función miembro pública) |
operator() (C++14) |
Devuelve value . (función miembro pública) |
Tipos miembro
Tipo | Definición |
value_type
|
bool
|
type
|
std::integral_constant<bool, value> |
[editar] Posible implementación
template< class T> struct is_default_constructible : std::is_constructible<T> {}; template< class T> struct is_trivially_default_constructible : std::is_trivially_constructible<T> {}; template< class T> struct is_nothrow_default_constructible : std::is_nothrow_constructible<T> {}; |
[editar] Notas
En varias implementaciones, is_nothrow_default_constructible
también comprueba si el destructor lanza porque es efectivamente noexcept(T()). Lo mismo aplica a is_trivially_default_constructible
, que, en estas implementaciones, también requiere que el destructor sea trivial: GCC bug 51452, Asunto LWG 2116.
std::is_default_constructible<T> no prueba que T x; compile; intenta direct-initialization con una lista de argumentos vacía (véase std::is_constructible). Por lo tanto, std::is_default_constructible_v<const int> y std::is_default_constructible_v<const int[10]> son true.
[editar] Ejemplo
#include <iostream> #include <type_traits> struct Ex1 { std::string str; // miembro no tiene un ctor por defecto no trivial }; struct Ex2 { int n; Ex2() = default; // trivial y no lanza }; int main() { std::cout << std::boolalpha << "Ex1 es construible por defecto? " << std::is_default_constructible<Ex1>::value << '\n' << "Ex1 es trivialmente construible por defecto? " << std::is_trivially_default_constructible<Ex1>::value << '\n' << "Ex2 es trivialmente construible por defecto? " << std::is_trivially_default_constructible<Ex2>::value << '\n' << "Ex2 es construible por defecto nothrow? " << std::is_nothrow_default_constructible<Ex2>::value << '\n'; }
Salida:
Ex1 es construible por defecto? true Ex1 es trivialmente construible por defecto? false Ex2 es trivialmente construible por defecto? true Ex2 es construible por defecto nothrow? true
[editar] Véase también
(C++11)(C++11)(C++11) |
Comprueba si un tipo tiene un constructor de argumentos concretos Original: checks if a type has a constructor for specific arguments The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (plantilla de clase) |
(C++11)(C++11)(C++11) |
Comprueba si un tipo tiene un constructor de copia Original: checks if a type has a copy constructor The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (plantilla de clase) |
(C++11)(C++11)(C++11) |
Comprueba si un tipo tiene un constructor de movimiento Original: checks if a type has a move constructor The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (plantilla de clase) |