std:: is_default_constructible, std:: is_trivially_default_constructible, std:: is_nothrow_default_constructible
|
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) |
value
bereit, die gleich
std::
is_trivially_constructible
<
T
>
::
value
ist.
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
=
|
(seit C++17) | |
|
template
<
class
T
>
inline
constexpr
bool
is_trivially_default_constructible_v
=
|
(seit C++17) | |
|
template
<
class
T
>
inline
constexpr
bool
is_nothrow_default_constructible_v
=
|
(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> {}; |
` 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
|
(C++11)
(C++11)
(C++11)
|
prüft, ob ein Typ einen Konstruktor für bestimmte Argumente besitzt
(Klassen-Template) |
|
(C++11)
(C++11)
(C++11)
|
prüft, ob ein Typ einen Kopierkonstruktor besitzt
(Klassen-Template) |
|
(C++11)
(C++11)
(C++11)
|
prüft, ob ein Typ aus einem Rvalue-Referenz konstruiert werden kann
(Klassen-Template) |
|
(C++20)
|
spezifiziert, dass ein Objekt eines Typs standardkonstruiert werden kann
(Konzept) |