std:: is_default_constructible, std:: is_trivially_default_constructible, std:: is_nothrow_default_constructible
|
Definido en el 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) |
Si
T
no es un tipo completo, (posiblemente calificado-cv)
void
, o un array de límite desconocido, el comportamiento es indefinido.
Si una instanciación de una plantilla anterior depende, directa o indirectamente, de un tipo incompleto, y esa instanciación podría producir un resultado diferente si ese tipo se completara hipotéticamente, el comportamiento no está definido.
Si el programa añade especializaciones para cualquiera de las plantillas descritas en esta página, el comportamiento es indefinido.
Contenidos |
Plantillas de variables auxiliares
|
template
<
class
T
>
inline
constexpr
bool
is_default_constructible_v
=
|
(desde C++17) | |
|
template
<
class
T
>
inline
constexpr
bool
is_trivially_default_constructible_v
=
|
(desde C++17) | |
|
template
<
class
T
>
inline
constexpr
bool
is_nothrow_default_constructible_v
=
|
(desde C++17) | |
Heredado de std:: integral_constant
Constantes miembro
|
value
[static]
|
true
si
T
es construible por defecto,
false
en caso contrario
(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 > |
Implementación posible
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> {}; |
Notas
En muchas implementaciones,
std::is_nothrow_default_constructible
también verifica si el destructor lanza excepciones porque es efectivamente
noexcept
(
T
(
)
)
. Lo mismo aplica para
std::is_trivially_default_constructible
, que, en estas implementaciones, también requiere que el destructor sea trivial:
GCC bug 51452
,
LWG issue 2116
.
std :: is_default_constructible < T > no prueba que T x ; compilaría; intenta realizar una inicialización directa con una lista de argumentos vacía (ver std::is_constructible ). Por lo tanto, std :: is_default_constructible_v < const int > y std :: is_default_constructible_v < const int [ 10 ] > son true .
Ejemplo
#include <string> #include <type_traits> struct S1 { std::string str; // el miembro tiene un constructor por defecto no trivial }; static_assert(std::is_default_constructible_v<S1> == true); static_assert(std::is_trivially_default_constructible_v<S1> == false); struct S2 { int n; S2() = default; // trivial y no lanzador }; static_assert(std::is_trivially_default_constructible_v<S2> == true); static_assert(std::is_nothrow_default_constructible_v<S2> == true); int main() {}
Véase también
|
(C++11)
(C++11)
(C++11)
|
verifica si un tipo tiene un constructor para argumentos específicos
(plantilla de clase) |
|
(C++11)
(C++11)
(C++11)
|
verifica si un tipo tiene un constructor de copia
(plantilla de clase) |
|
(C++11)
(C++11)
(C++11)
|
verifica si un tipo puede ser construido desde una referencia a valor
(plantilla de clase) |
|
(C++20)
|
especifica que un objeto de un tipo puede ser construido por defecto
(concepto) |