std::optional<T>:: optional
|
constexpr
optional
(
)
noexcept
;
|
(1) | (seit C++17) |
|
constexpr
optional
(
std::
nullopt_t
)
noexcept
;
|
(2) | (seit C++17) |
|
constexpr
optional
(
const
optional
&
other
)
;
|
(3) | (seit C++17) |
|
constexpr
optional
(
optional
&&
other
)
noexcept
(
/* siehe unten */
)
;
|
(4) | (seit C++17) |
|
template
<
class
U
>
optional ( const optional < U > & other ) ; |
(5) |
(seit C++17)
(constexpr seit C++20) (bedingt explizit) |
|
template
<
class
U
>
optional ( optional < U > && other ) ; |
(6) |
(seit C++17)
(constexpr seit C++20) (bedingt explizit) |
|
template
<
class
...
Args
>
constexpr explicit optional ( std:: in_place_t , Args && ... args ) ; |
(7) | (seit C++17) |
|
template
<
class
U,
class
...
Args
>
constexpr
explicit
optional
(
std::
in_place_t
,
|
(8) | (seit C++17) |
|
template
<
class
U
=
std::
remove_cv_t
<
T
>
>
constexpr optional ( U && value ) ; |
(9) |
(seit C++17)
(bedingt explizit) |
Konstruiert ein neues
optional
-Objekt.
Inhaltsverzeichnis |
Parameter
| other | - |
ein anderes
optional
Objekt, dessen enthaltene Wert kopiert wird
|
| value | - | Wert, mit dem der enthaltene Wert initialisiert wird |
| args... | - | Argumente, mit denen der enthaltene Wert initialisiert wird |
| ilist | - | Initialisierungsliste, mit der der enthaltene Wert initialisiert wird |
Effekte
| Überladung | Initialisierungsmethode | Initialisierer für den enthaltenen Wert |
has_value()
nach Konstruktion
|
|---|---|---|---|
| ( 1 ) | N/A | - | false |
| ( 2 ) | |||
| ( 3 ) | Direkte (non-list) | * other |
other.
has_value
(
)
|
| ( 4 ) | std :: move ( * other ) | ||
| ( 5 ) | * other | ||
| ( 6 ) | std :: move ( * other ) | ||
| ( 7 ) | std:: forward < Args > ( args ) ... | true | |
| ( 8 ) | ilist, std:: forward < Args > ( args ) ... | ||
| ( 9 ) | std:: forward < U > ( value ) |
Einschränkungen und ergänzende Informationen
- std:: is_constructible_v < T, const U & > ist true .
-
Falls
Tnicht (möglicherweise cv-qualifiziert) bool ist, sind alle folgenden 8 Werte false [1] :- std:: is_constructible_v < T, std:: optional < U > & >
- std:: is_constructible_v < T, const std:: optional < U > & >
- std:: is_constructible_v < T, std:: optional < U > && >
- std:: is_constructible_v < T, const std:: optional < U > && >
- std:: is_convertible_v < std:: optional < U > & , T >
- std:: is_convertible_v < const std:: optional < U > & , T >
- std:: is_convertible_v < std:: optional < U > && , T >
- std:: is_convertible_v < const std:: optional < U > && , T >
- std:: is_constructible_v < T, U > ist true .
-
Falls
Tnicht (möglicherweise cv-qualifiziert) bool ist, sind die folgenden 8 Werte alle false [1] :- std:: is_constructible_v < T, std:: optional < U > & >
- std:: is_constructible_v < T, const std:: optional < U > & >
- std:: is_constructible_v < T, std:: optional < U > && >
- std:: is_constructible_v < T, const std:: optional < U > && >
- std:: is_convertible_v < std:: optional < U > & , T >
- std:: is_convertible_v < const std:: optional < U > & , T >
- std:: is_convertible_v < std:: optional < U > && , T >
- std:: is_convertible_v < const std:: optional < U > && , T >
T
’s für die Initialisierung ausgewählter Konstruktor ein
constexpr
-Konstruktor ist, dann ist dieser Konstruktor ebenfalls ein
constexpr
-Konstruktor.
T
’s für die Initialisierung ausgewählter Konstruktor ein
constexpr
-Konstruktor ist, dann ist dieser Konstruktor ebenfalls ein
constexpr
-Konstruktor.
- std:: is_constructible_v < T, U > ist true .
- std:: decay_t < U > (bis C++20) std:: remove_cvref_t < U > (seit C++20) weder std::in_place_t noch std:: optional < T > ist.
-
Wenn
T(möglicherweise cv-qualifiziert) bool ist, std:: decay_t < U > (bis C++20) std:: remove_cvref_t < U > (seit C++20) keine Spezialisierung vonstd::optionalist.
T
’s für die Initialisierung ausgewählter Konstruktor ein
constexpr
-Konstruktor ist, dann ist dieser Konstruktor ebenfalls ein
constexpr
-Konstruktor.
-
↑
1.0
1.1
Mit anderen Worten,
Tist weder konstruierbar noch konvertierbar von irgendeinem Ausdruck des Typs (möglicherweise const-qualifiziert) std:: optional < U >
Exceptions
T
geworfen wird.
T
geworfen wird. Hat die folgende
T
geworfen wird.
Deduktionshilfen
Hinweise
Vor der Lösung von
LWG Issue 3836
wählte die Konstruktion eines
std::
optional
<
bool
>
aus
std::
optional
<
U
>
Überladung
(
9
)
anstelle der Überladungen
(
5,6
)
, falls
U
nicht
bool
ist. Dies liegt daran, dass die Überladungen
(
5,6
)
nicht an der Überladungsauflösung teilnahmen, wenn
T
(in diesem Fall
bool
) von
std::
optional
<
U
>
konstruiert oder konvertiert werden kann, aber
std::optional::operator bool
die Konversion für jedes
U
ermöglicht.
Folglich enthält das konstruierte std:: optional < bool > immer einen Wert. Dieser Wert wird dadurch bestimmt, ob das bereitgestellte std:: optional < U > Objekt einen Wert enthält, und nicht durch den bool Wert, der direkt aus dem enthaltenen Wert initialisiert wurde:
std::optional<bool> op_false(false); std::optional<int> op_zero(0); std::optional<int> from_bool(op_false); // OK: enthält 0 (initialisiert von false) std::optional<bool> from_int(op_zero); // DEFEKT (LWG 3836): enthält true, weil // op_zero einen Wert enthält, selbst wenn die // Initialisierung von bool mit diesem Wert false ergibt
| Feature-Test Makro | Wert | Std | Feature |
|---|---|---|---|
__cpp_lib_optional
|
202106L
|
(C++20)
(DR20) |
Vollständig constexpr ( 5,6 ) |
Beispiel
#include <iostream> #include <optional> #include <string> int main() { std::optional<int> o1, // leer o2 = 1, // Initialisierung aus Rvalue o3 = o2; // Kopierkonstruktor // Ruft std::string( initializer_list<CharT> ) Konstruktor auf std::optional<std::string> o4(std::in_place, {'a', 'b', 'c'}); // Ruft std::string( size_type count, CharT ch ) Konstruktor auf std::optional<std::string> o5(std::in_place, 3, 'A'); // Move-konstruiert von std::string unter Verwendung des Deduction Guide zur Typauswahl std::optional o6(std::string{"deduction"}); std::cout << *o2 << ' ' << *o3 << ' ' << *o4 << ' ' << *o5 << ' ' << *o6 << '\n'; }
Ausgabe:
1 1 abc AAA deduction
Fehlerberichte
Die folgenden verhaltensändernden Fehlerberichte wurden rückwirkend auf zuvor veröffentlichte C++-Standards angewendet.
| DR | Angewendet auf | Verhalten wie veröffentlicht | Korrigiertes Verhalten |
|---|---|---|---|
| LWG 3836 | C++17 |
beim Konstruieren eines
std::
optional
<
bool
>
aus std:: optional < U > würde die Überladungsauflösung Überladung ( 9 ) auswählen, falls
U
nicht
bool
ist
|
wählt in diesem Fall immer den
konvertierenden Kopier-/Move- Konstruktor aus |
| LWG 3886 | C++17 |
das Standard-Template-Argument von Überladung
(
9
)
war
T
|
geändert zu std:: remove_cv_t < T > |
| P0602R4 | C++17 |
Kopier-/Move-Konstruktoren könnten nicht trivial sein
selbst wenn der zugrundeliegende Konstruktor trivial ist |
muss
Trivialität weitergeben |
| P2231R1 | C++20 |
Überladungen
(
5,6
)
von einem anderen
std::optional
waren nicht
constexpr
|
als constexpr festgelegt |
Siehe auch
|
(C++17)
|
erstellt ein
optional
-Objekt
(Funktions-Template) |