std:: is_constructible, std:: is_trivially_constructible, std:: is_nothrow_constructible
|
Definiert im Header
<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 die Member-Konstante
value
den Wert
true
. In allen anderen Fällen ist
value
false
.
Für diese Überprüfung wird die Variablendefinition niemals als Funktionsdeklaration interpretiert, und die Verwendung von std::declval wird nicht als ODR-Nutzung betrachtet. Zugriffsprüfungen werden durchgeführt, als ob sie aus einem Kontext stammten, der nicht mit
T
oder einem der Typen in
Args
in Beziehung steht. Nur die Gültigkeit des unmittelbaren Kontexts der Variablendefinition wird berücksichtigt.
noexcept
.
Wenn
T
oder irgendein Typ im Parameterpaket
Args
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 irgendwelche der auf dieser Seite beschriebenen Templates hinzufügt, ist das Verhalten undefiniert.
Inhaltsverzeichnis |
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) | |
Geerbt von std:: integral_constant
Member-Konstanten
|
value
[static]
|
true
falls
T
konstruierbar ist aus
Args...
,
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 > |
Hinweise
In vielen Implementierungen prüft
is_nothrow_constructible
auch, ob der Destruktor wirft, da es effektiv
noexcept
(
T
(
arg
)
)
ist. Gleiches gilt für
is_trivially_constructible
, das in diesen Implementierungen auch erfordert, dass der Destruktor trivial ist:
GCC Bug 51452
LWG Issue 2116
.
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
Siehe auch
|
(C++11)
(C++11)
(C++11)
|
prüft, ob ein Typ einen Standardkonstruktor besitzt
(Klassentemplate) |
|
(C++11)
(C++11)
(C++11)
|
prüft, ob ein Typ einen Kopierkonstruktor besitzt
(Klassentemplate) |
|
(C++11)
(C++11)
(C++11)
|
prüft, ob ein Typ aus einem Rvalue-Referenz konstruiert werden kann
(Klassentemplate) |
|
(C++20)
|
spezifiziert, dass eine Variable des Typs aus oder an eine Menge von Argumenttypen konstruiert oder gebunden werden kann
(Konzept) |