std::any::has_value
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Utilities library
Type support (basic types, RTTI)
Library feature-test macros (C++20)
(C++11)
(C++20)
(C++26)
(C++20)
Coroutine support (C++20)
Contract support (C++26)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
Relational operators (deprecated in C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
(C++20)
Swap and type operations
Common vocabulary types
bool has_value() const noexcept;
(since C++17)
Checks whether the object contains a value.
Contents
[edit] Parameters
(none)
[edit] Return value
true if and only if the instance contains a value.
[edit] Example
Run this code
#include <any> #include <cassert> #include <string> int main() { std::any a0; assert (a0.has_value() == false); std::any a1 = 42; assert (a1.has_value() == true); assert (std::any_cast <int>(a1) == 42); a1.reset(); assert (a1.has_value() == false); auto a2 = std::make_any <std::string >("Andromeda"); assert (a2.has_value() == true); assert (std::any_cast <std::string &>(a2) == "Andromeda"); a2.reset(); assert (a2.has_value() == false); }