If I put a T
into an std::any
, I can get it with any_cast<T>(my_any)
. But does the standard (= C++17, in the ballot phase at the moment) include a function like any_cast<T>(optional<any> oa)
which returns nullopt
if oa is nullopt
and std::any_cast<T>(oa.value())
otherwise? Or something along those lines?
Edit: Since people seem to be suggesting implementations, I'll also list what I use for now:
/* using magic here to select between boost/std::experimental/std versions */
template<typename T>
inline const optional<T> any_cast(const optional<any>& operand)
{
return operand ?
optional<T>(any_cast<T>(operand.value())) :
optional<T>(nullopt);
}