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
Definiert im Header <type_traits>
template < class T >
struct is_default_constructible ;
(1) (seit C++11)
template < class T >
struct is_trivially_default_constructible ;
(2) (seit C++11)
template < class T >
struct is_nothrow_default_constructible ;
(3) (seit C++11)
1) Stellt die Member-Konstante value bereit, die gleich std:: is_constructible < T > :: value ist.
2) Stellt die Member-Konstante value bereit, die gleich std:: is_trivially_constructible < T > :: value ist.
3) Stellt die Member-Konstante value bereit, die gleich std:: is_nothrow_constructible < T > :: value ist.

Wenn T kein vollständiger Typ ist, (möglicherweise cv-qualifiziert) void , oder ein Array unbekannter Größe, ist das Verhalten undefiniert.

Wenn eine Instanziierung einer Vorlage oben direkt oder indirekt von einem unvollständigen Typ abhängt und diese Instanziierung ein anderes Ergebnis liefern könnte, wenn dieser Typ hypothetisch vervollständigt würde, ist das Verhalten undefiniert.

Wenn das Programm Spezialisierungen für irgendeine der auf dieser Seite beschriebenen Templates hinzufügt, ist das Verhalten undefiniert.

Inhaltsverzeichnis

Hilfsvariablen-Templates

template < class T >

inline constexpr bool is_default_constructible_v =

is_default_constructible < T > :: value ;
(seit C++17)
template < class T >

inline constexpr bool is_trivially_default_constructible_v =

is_trivially_default_constructible < T > :: value ;
(seit C++17)
template < class T >

inline constexpr bool is_nothrow_default_constructible_v =

is_nothrow_default_constructible < T > :: value ;
(seit C++17)

Geerbt von std:: integral_constant

Member-Konstanten

value
[static]
true wenn T standardkonstruierbar ist, false andernfalls
(öffentliche statische Member-Konstante)

Member-Funktionen

operator bool
konvertiert das Objekt zu bool , gibt value zurück
(öffentliche Member-Funktion)
operator()
(C++14)
gibt value zurück
(öffentliche Member-Funktion)

Member-Typen

Typ Definition
value_type bool
type std:: integral_constant < bool , value >

Mögliche Implementierung

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> {};
**Hinweis:** Der gesamte Code-Block innerhalb der `
` Tags wurde gemäß den Anweisungen nicht übersetzt, da es sich um C++-Code handelt. Die HTML-Struktur und Attribute bleiben ebenfalls unverändert.

Hinweise

In vielen Implementierungen prüft std::is_nothrow_default_constructible auch, ob der Destruktor wirft, da es effektiv noexcept ( T ( ) ) ist. Gleiches gilt für std::is_trivially_default_constructible , das in diesen Implementierungen ebenfalls erfordert, dass der Destruktor trivial ist: GCC Bug 51452 , LWG Issue 2116 .

std :: is_default_constructible < T > testet nicht, ob T x ; kompilieren würde; es versucht Direktinitialisierung mit einer leeren Argumentliste (siehe std::is_constructible ). Daher sind std :: is_default_constructible_v < const int > und std :: is_default_constructible_v < const int [ 10 ] > true .

Beispiel

#include <string>
#include <type_traits>
struct S1
{
    std::string str; // Member hat einen nicht-trivialen Standardkonstruktor
};
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 und nicht-werfend
};
static_assert(std::is_trivially_default_constructible_v<S2> == true);
static_assert(std::is_nothrow_default_constructible_v<S2> == true);
int main() {}

Siehe auch

prüft, ob ein Typ einen Konstruktor für bestimmte Argumente besitzt
(Klassen-Template)
prüft, ob ein Typ einen Kopierkonstruktor besitzt
(Klassen-Template)
prüft, ob ein Typ aus einem Rvalue-Referenz konstruiert werden kann
(Klassen-Template)
spezifiziert, dass ein Objekt eines Typs standardkonstruiert werden kann
(Konzept)