Namespaces
Variants

std:: is_default_constructible, std:: is_trivially_default_constructible, std:: is_nothrow_default_constructible

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
is_default_constructible is_trivially_default_constructible is_nothrow_default_constructible
(C++11) (C++11) (C++11)
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
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)
1) Proporciona la constante miembro value igual a std:: is_constructible < T > :: value .
2) Proporciona la constante miembro value igual a std:: is_trivially_constructible < T > :: value .
3) Proporciona la constante miembro value igual a std:: is_nothrow_constructible < T > :: value .

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 =

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
[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

verifica si un tipo tiene un constructor para argumentos específicos
(plantilla de clase)
verifica si un tipo tiene un constructor de copia
(plantilla de clase)
verifica si un tipo puede ser construido desde una referencia a valor
(plantilla de clase)
especifica que un objeto de un tipo puede ser construido por defecto
(concepto)