相关疑难解决方法(0)

为什么要使用完美转发的值(仿函数)?

C++ 11(和C++ 14)引入了针对通用编程的其他语言结构和改进.这些功能包括:

  • R值参考
  • 参考折叠
  • 完美转发
  • 移动语义,可变参数模板等

我在浏览较早草案的的C++ 14规范(现在更新文本)和码中的示例中§20.5.1,编译时整数序列,我发现有趣和奇特.

template<class F, class Tuple, std::size_t... I>
decltype(auto) apply_impl(F&& f, Tuple&& t, index_sequence<I...>) {
  return std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...);
}

template<class F, class Tuple>
decltype(auto) apply(F&& f, Tuple&& t) {
  using Indices = make_index_sequence<std::tuple_size<Tuple>::value>;
  return apply_impl(std::forward<F>(f), std::forward<Tuple>(t), Indices());
}
Run Code Online (Sandbox Code Playgroud)

在线这里[intseq.general]/2.

  • 为什么功能fapply_impl被转发,即为什么std::forward<F>(f)(std::get...
  • 为什么不直接应用该功能f(std::get...

c++ perfect-forwarding c++11 c++14

32
推荐指数
2
解决办法
1521
查看次数

标签 统计

c++ ×1

c++11 ×1

c++14 ×1

perfect-forwarding ×1