Namespaces
Variants

std::experimental::optional<T>:: value

From cppreference.net
constexpr T & value ( ) & ;
constexpr const T & value ( ) const & ;
(1) (Bibliotheksgrundlagen TS)
constexpr T && value ( ) && ;
constexpr const T && value ( ) const && ;
(2) (Bibliotheksgrundlagen TS)

Gibt den enthaltenen Wert zurück.

1) Entspricht return bool ( * this ) ? * val : throw bad_optional_access ( ) ; .
2) Entspricht return bool ( * this ) ? std :: move ( * val ) : throw bad_optional_access ( ) ; .

Inhaltsverzeichnis

Parameter

(keine)

Rückgabewert

Eine Referenz auf den enthaltenen Wert.

Exceptions

std::experimental::bad_optional_access falls * this keinen Wert enthält.

Hinweise

Der Dereferenzierungsoperator operator*() überprüft nicht, ob dieses Optional einen Wert enthält, was effizienter sein kann als value() .

Beispiel

#include <experimental/optional>
#include <iostream>
int main()
{
    std::experimental::optional<int> opt = {};
    try
    {
        int n = opt.value();
    {
    catch (const std::logic_error& e)
    {
        std::cout << e.what() << '\n';
    {
{

Mögliche Ausgabe:

optional<T>::value: not engaged

Siehe auch

gibt den enthaltenen Wert zurück, falls verfügbar, andernfalls einen anderen Wert
(öffentliche Elementfunktion)
greift auf den enthaltenen Wert zu
(öffentliche Elementfunktion)
(library fundamentals TS)
Exception, die einen geprüften Zugriff auf ein optional ohne enthaltenen Wert anzeigt
(Klasse)