Namespaces
Variants

std:: is_constructible, std:: is_trivially_constructible, std:: is_nothrow_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
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, 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)
1) Falls 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.
2) Gleich wie (1) , aber die Variablendefinition ruft keine Operation auf, die nicht trivial ist. Für diese Prüfung wird der Aufruf von std::declval als trivial betrachtet.
3) Gleich wie (1) , aber die Variablendefinition ist 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 =

is_constructible < T, Args... > :: value ;
(seit C++17)
template < class T, class ... Args >

inline constexpr bool is_trivially_constructible_v =

is_trivially_constructible < T, Args... > :: value ;
(seit C++17)
template < class T, class ... Args >

inline constexpr bool is_nothrow_constructible_v =

is_nothrow_constructible < T, Args... > :: value ;
(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

prüft, ob ein Typ einen Standardkonstruktor besitzt
(Klassentemplate)
prüft, ob ein Typ einen Kopierkonstruktor besitzt
(Klassentemplate)
prüft, ob ein Typ aus einem Rvalue-Referenz konstruiert werden kann
(Klassentemplate)
spezifiziert, dass eine Variable des Typs aus oder an eine Menge von Argumenttypen konstruiert oder gebunden werden kann
(Konzept)