Espacios de nombres
Variantes
Acciones

std::optional<T>::operator bool, std::optional<T>::has_value

De cppreference.com
< cpp‎ | utility‎ | optional
 
 
Biblioteca de servicios
 
 
constexpr explicit operator bool() const noexcept;
(desde C++17)
constexpr bool has_value() const noexcept;
(desde C++17)

Comprueba si *this contiene un valor.

[editar] Parámetros

(Ninguno)

[editar] Valor de retorno

true si *this contiene un valor, false si *this no contiene un valor.

[editar] Ejemplo

#include <optional>
#include <iostream>
 
int main()
{
    std::cout << std::boolalpha;
 
    std::optional<int> opt;
    std::cout << opt.has_value() << '\n';
 
    opt = 43;
    if (opt)
        std::cout << "valor establecido a " << opt.value() << '\n';
    else
        std::cout << "valor no establecido\n";
 
    opt.reset();
    if (opt.has_value())
        std::cout << "valor aún establecido a " << opt.value() << '\n';
    else
        std::cout << "valor ya no está establecido\n";
}

Salida:

false
valor establecido a 43
valor ya no está establecido