std::is_constructible, std::is_trivially_constructible, std::is_nothrow_constructible
| Definiert in der Kopfdatei <type_traits> |
||
| template< class T, class... Args > struct is_constructible; |
(1) | (seit C++11) |
| template< class T, class... Args > struct is_trivially_constructible; |
(2) | (seit C++11) |
| template< class T, class... Args > struct is_nothrow_constructible; |
(3) | (seit C++11) |
T ein Objekt- oder Referenztyp ist und die Variablendefinition T obj(std::declval<Args>()...); wohlgeformt ist, liefert sie das Member-Konstante value gleich true. In allen anderen Fällen ist value false.Zu diesem Zweck wird die Variablendefinition niemals als Funktionsdeklaration interpretiert, und die Verwendung von std::declval gilt nicht als ODR-Verwendung. Zugriffsprüfungen werden so durchgeführt, als kämen sie aus einem Kontext, der nicht mit
T und keinem der Typen in Args zusammenhängt. Nur die Gültigkeit des unmittelbaren Kontexts der Variablendefinition wird berücksichtigt.noexcept.Wenn T oder ein Typ im Parameter-Pack Args kein vollständiger Typ ist, (möglicherweise cv-qualifiziert) void oder ein Array ohne bekannte Begrenzung, ist das Verhalten undefiniert.
Wenn eine Instanziierung einer Vorlage davon 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 eine der auf dieser Seite beschriebenen Vorlagen hinzufügt, ist das Verhalten undefiniert.
Inhalt |
[edit] Hilfsvariablen-Templates
| template< class T, class... Args > inline constexpr bool is_constructible_v = |
(seit C++17) | |
| template< class T, class... Args > inline constexpr bool is_trivially_constructible_v = |
(seit C++17) | |
| template< class T, class... Args > inline constexpr bool is_nothrow_constructible_v = |
(seit C++17) | |
Abgeleitet von std::integral_constant
Member-Konstanten
| value [static] |
true, wenn T aus Args... konstruierbar ist, false sonst(öffentliche statische Member-Konstante) |
Memberfunktionen
| operator bool |
konvertiert das Objekt zu bool, gibt value zurück (öffentliche Memberfunktion) |
| operator() (C++14) |
gibt value zurück (öffentliche Memberfunktion) |
Membertypen
| Typ | Definition |
value_type
|
bool |
type
|
std::integral_constant<bool, value> |
[edit] Anmerkungen
In vielen Implementierungen prüft is_nothrow_constructible auch, ob der Destruktor wirft, da er effektiv noexcept(T(arg)) ist. Dasselbe gilt für is_trivially_constructible, das in diesen Implementierungen auch einen trivialen Destruktor erfordert: GCC Bug 51452 LWG Issue 2116.
[edit] Beispiel
#include <iostream> #include <type_traits> class Foo { int v1; double v2; public: Foo(int n) : v1(n), v2() {} Foo(int n, double f) noexcept : v1(n), v2(f) {} }; int main() { auto is = [](bool o) { return (o ? "\t" "is " : "\t" "isn't "); }; std::cout << "Foo ...\n" << is(std::is_trivially_constructible_v<Foo, const Foo&>) << "Trivially-constructible from const Foo&\n" << is(std::is_trivially_constructible_v<Foo, int>) << "Trivially-constructible from int\n" << is(std::is_constructible_v<Foo, int>) << "Constructible from int\n" << is(std::is_nothrow_constructible_v<Foo, int>) << "Nothrow-constructible from int\n" << is(std::is_nothrow_constructible_v<Foo, int, double>) << "Nothrow-constructible from int and double\n"; }
Ausgabe
Foo ...
is Trivially-constructible from const Foo&
isn't Trivially-constructible from int
is Constructible from int
isn't Nothrow-constructible from int
is Nothrow-constructible from int and double[edit] Siehe auch
| prüft, ob ein Typ einen Standardkonstruktor hat (Klassenvorlage) | |
| (C++11)(C++11)(C++11) |
prüft, ob ein Typ einen Kopierkonstruktor hat (Klassenvorlage) |
| (C++11)(C++11)(C++11) |
prüft, ob ein Typ aus einer rvalue-Referenz konstruiert werden kann (Klassenvorlage) |
| (C++20) |
gibt an, dass eine Variable des Typs aus einer Reihe von Argumenttypen konstruiert oder an sie gebunden werden kann (Konzept) |