std::any_cast (3) - Linux Manuals
std::any_cast: std::any_cast
NAME
Synopsis
template<class T> (1) (since C++17)
T any_cast(const any& operand);
template<class T> (2) (since C++17)
T any_cast(any& operand);
template<class T> (3) (since C++17)
T any_cast(any&& operand);
template<class T> (4) (since C++17)
const T* any_cast(const any* operand) noexcept;
template<class T> (5) (since C++17)
T* any_cast(any* operand) noexcept;
Performs type-safe access to the contained object.
Let U be std::remove_cv_t<std::remove_reference_t<T>>.
1) The program is ill-formed if std::is_constructible_v<T, const U&> is false.
2) The program is ill-formed if std::is_constructible_v<T, U&> is false.
3) The program is ill-formed if std::is_constructible_v<T, U> is false.
Parameters
operand - target any object
Return value
1-2) Returns static_cast<T>(*std::any_cast<U>(&operand))
3) Returns static_cast<T>(std::move(*std::any_cast<U>(&operand))).
4-5) If operand is not a null pointer, and the typeid of the requested T matches that of the contents of operand, a pointer to the value contained by operand, otherwise a null pointer.
Exceptions
1-3) Throws std::bad_any_cast if the typeid of the requested T does not match that of the contents of operand.
Example
// Run this code
Possible output: