C++17使用std::apply和fold expression对std::tuple进行遍历
std::apply函数
先来看这个std::apply
函数,这个函数定义在tuple
头文件中,函数签名如下:
template <class F, class Tuple>
constexpr decltype(auto) apply(F&& f, Tuple&& t);
该函数接受两个参数,第一个是一个函数对象,第二个是一个Tuple对象
来看一个最简单的示例:
#include <tuple>
#include <iostream>
int main() {
// 两个元素相加
std::cout << std::apply([](auto x, auto y) { return x + y; },
std::make_tuple(1, 2.0)) << '\n';
}
输出结果是3
这个例子中第一个参数使用Lambda匿名函数将tuple
中的两个元素相加,第二个使用std::make_tuple
函数构造一个只含有两个元素的tuple
fold expression
这个特性是C++ 17中我觉得很有用的一个新特性,使用规则有下面四条:
-
Unary right fold ( …) becomes ( (… ( )))
-
Unary left fold (… ) becomes ((( ) …) )
-
Binary right fold ( … ) becomes ( (… ( ( ))))
-
Binary left fold ( … ) becomes (((( ) ) …) )
这里的 指的是Expression(符合C++语法的表达式), 指的是operator(操作符), 是parameter pack(可变参数)的个数, 是一个常数。
可能看这个规则有些抽象,我们来看一些具体的例子:
#include <tuple>
#include <iostream>
int main() {
// 多个元素相加,使用parameter pack
std::cout << std::apply([](auto&& ... args) { return (args + ...); },
std::make_tuple(1, 2.f, 3.0)) << '\n';
// 遍历tuple并输出,注意逗号操作符的使用
std::apply([](auto&&... args) { ((std::cout << args << '\n'), ...); },
std::make_tuple(1, 2.f, 3.0));
}
输出如下:
6
1
2
3
第6行中,std::apply
函数的第一个参数是一个Lambda匿名函数,函数的参数是一个可变参数args
,函数体中只有一条语句args + ...
,这个情况就是上面的第一种情况:这里的
就是args
,
就是+
,所以展开来就是
(因为参数的个数是3)。
第9行中,Lambda匿名函数的函数体是((std::cout << args << '\n'), ...)
这是一个逗号操作符,也属于上面四种情况中的第一种:这里的
就是std::cout << args << '\n')
,
就是,
,所以这一行就打印输出了tuple
的每一个元素。如果在C++17之前想要遍历tuple
就比较麻烦,需要很多额外的操作。