std::optional<T>::operator bool, std::optional<T>::has_value - cppreference.com
De cppreference.com
|
|
(desde C++17) | |
|
|
(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
#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