【发布时间】:2016-12-21 10:34:34
【问题描述】:
考虑std::apply的可能实现:
namespace detail {
template <class F, class Tuple, std::size_t... I>
constexpr decltype(auto) apply_impl(F &&f, Tuple &&t, std::index_sequence<I...>)
{
return std::invoke(std::forward<F>(f), std::get<I>(std::forward<Tuple>(t))...);
}
} // namespace detail
template <class F, class Tuple>
constexpr decltype(auto) apply(F &&f, Tuple &&t)
{
return detail::apply_impl(
std::forward<F>(f), std::forward<Tuple>(t),
std::make_index_sequence<std::tuple_size_v<std::decay_t<Tuple>>>{});
}
为什么在调用函数(f) 和参数元组传递(t) 时,我们不需要在实现中对元组std::get<I>(std::forward<Tuple>(t))... 的每个元素执行std::forward?
【问题讨论】:
标签: c++ c++17 perfect-forwarding stdapply