std::optional<T>::operator bool, std::optional<T>::has_value
De cppreference.com
<tbody>
</tbody>
constexpr explicit operator bool() const noexcept; |
(desde C++17) | |
constexpr bool has_value() const noexcept; |
(desde C++17) | |
Comprueba si *this contiene un valor.
Parámetros
(Ninguno)
Valor de retorno
true si *this contiene un valor, false si *this no contiene un valor.
Ejemplo
Ejecuta este código
#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