您可以定义以下函数:
namespace detail
{
template<typename Callable, typename T>
struct apply_helper
{
using T_noref = typename std::remove_reference<T>::type;
using value_type = typename T_noref::value_type;
using Callable_return = decltype(std::declval<Callable>()(std::declval<value_type>()));
using return_type = optional<Callable_return>;
static return_type eval(Callable&& f, T&& val)
{
if(val)
{
return apply(std::forward<Callable&&>(f), *val);
}
else return boost::none;
}
private:
static Callable_return apply(Callable&& f, value_type& v)
{
return f(v);
}
static Callable_return apply(Callable&& f, value_type const& v)
{
return f(v);
}
static Callable_return apply(Callable&& f, value_type&& v)
{
return f(v);
}
};
}
template<typename Callable, typename T>
optional<decltype(std::declval<Callable>()(std::declval<T>()))> apply(Callable&& f, optional<T> const& a)
{
return detail::apply_helper<Callable, optional<T> const&>::eval(std::forward<Callable>(f), a);
}
然后可以像这样使用:
optional<int> foo(optional<int> value)
{
auto f = [](int v){return v + 10;};
return apply(f, value);
}