🐭 Variadic Templates 与 Fold Expressions

现代 C++ 中可变参数模板 (Variadic Templates) 的引入,使得设计一个可以接收任意多个,任意类型参数的通用函数成为可能。标准库里面的 tuple 就是通过可变参数模板递归来实现的。折叠表达式 (Fold Expressions) 的引入进一步简化了实现的复杂度,用起来更加自然了。
折叠表达式展开规则:

表达式 类型 展开
(… op e) unary left fold ((e1 op e2) op …) op eN
(e op …) unary right fold e1 op (… op (eN-1 op eN))
**(init op … op e)** binary left fold (((init op e1) op e2) op …) op eN
(e op … op init) binary right fold e1 op (… op (eN-1 op (eN op init)))

:::tips 注意这里总结的折叠表达式的展开过程,其实还是很好理解的,就是一个递归过程,一般使用右折叠的情况比较多,从左到右逐个处理传入的参数,比较符合人的认知习惯。 ::: 代码示例 :

  1. #include <iostream>
  2. // fold expression, the ... dots
  3. // on the right-hand side of the operator, called a right fold
  4. // on the left- hand side of the operator, called a left fold
  5. // fold expression (op is ',')
  6. template <typename... Args> void print(Args... args) {
  7. ((std::cout << args << ' '), ...);
  8. std::cout << "\n";
  9. }
  10. // fold expression (op is '=')
  11. template <typename... Args> void rprint(Args... args) {
  12. int dummy{};
  13. ((std::cout << args << ' ', dummy) = ... = 0);
  14. std::cout << "\n";
  15. }
  16. // fold expression (op is '&&')
  17. template <typename T, typename... Ts> bool within(T min, T max, Ts... ts) {
  18. return ((min <= ts && ts <= max) && ...);
  19. }
  20. // fold expression (op is '+')
  21. template <typename R, typename... Ts> auto matches(const R &range, Ts... ts) {
  22. return (std::count(std::begin(range), std::end(range), ts) + ...);
  23. }
  24. int main() {
  25. print(1, 2.2, "3.3"); // 1 2.2 3.3
  26. rprint(1, 2.2, "3.3"); // 3.3 2.2 1
  27. std::cout << std::boolalpha << within(10, 20, 15, 30) << '\n'; // false
  28. std::cout << matches("abcdefg", 'a', 'd', 'f') << '\n'; // 3
  29. return 0;
  30. }

🐂 Lambda 与完美转发

我们知道模板函数结合完美转发可以设计非常通用的函数模板:

  1. T&& 类型推导
  2. std::forward(t) 转发参数

其实 Lambda 也可以结合完美转发设计非常通用的 Lambda:

  1. auto && 类型推导
  2. std::forward(t) 转发参数

代码示例 :

  1. #include <iostream>
  2. class X {};
  3. void func(X &&x) { std::cout << "calls func(X &&x)\n"; }
  4. void func(X &x) { std::cout << "calls func(X &x)\n"; }
  5. void func(const X &x) { std::cout << "calls func(const X &x)\n"; }
  6. void func(volatile X &x) { std::cout << "calls func(volatile X &x)\n"; }
  7. auto fwd1 = [](auto &&... params) {
  8. // accept any type of arguments and forward them to func
  9. return func(std::forward<decltype(params)>(params)...);
  10. };
  11. template <typename... Ts> auto fwd2(Ts &&... params) {
  12. // accept any type of arguments and forward them to func
  13. return func(std::forward<Ts>(params)...);
  14. }
  15. int main() {
  16. // fwd can keeps the passed in value's cvr qualifier
  17. X x00{}, x01{};
  18. X x1{};
  19. const X x2{};
  20. volatile X x3{};
  21. fwd1(std::move(x00)); // calls func(X &&x)
  22. fwd1(x1); // calls func(X &x)
  23. fwd1(x2); // calls func(const X &x)
  24. fwd1(x3); // calls func(volatile X &x)
  25. fwd2(std::move(x01)); // calls func(X &&x)
  26. fwd2(x1); // calls func(X &x)
  27. fwd2(x2); // calls func(const X &x)
  28. fwd2(x3); // calls func(volatile X &x)
  29. return 0;
  30. }

:::tips 完美转发结合折叠表达式进而设计出通用函数模板与通用 Lambda 表达式 :::

🐯 总结

  1. 使用 std::forward 结合 decltype 转发 auto&& Lambda 参数
  2. 理解折叠表达式的展开规则,应用折叠表达式设计通用函数与通用 Lambda