I suspect boost::optional's get_value_or
was deprecated because it is unsafe if an rvalue is passed as the default
parameter. However, it is occasionally useful to be able to reference the optional value or a default alternative.
Is the following safe?
template<typename T>
T const& get_reference_or(boost::optional<T> const& opt, T const& alt)
{
if (opt) return opt.get();
else return alt;
}
template<typename T>
T const& get_reference_or(boost::optional<T> const&, T&&) = delete;