<>C++17之std::apply与std::make_from_tuple

C++17中有两个有意思的语法,是关于std::tuple(或std::pair、std::array等可以通过std::get
来操作成员的元组容器)与函数参数转化的问题的。今天这篇文章我们来研究一下。

<>std::apply

std::apply最主要的作用就是把tuple转化为函数参数,然后去调用函数。如果没有STL,我们需要自行去实现,首先需要实现一个sequence
,里面是从0到size,分别对应tuple中的每一个参数,然后再通过std::get和折叠展开语法来把tuple中的数据展开到参数列表中,比如这样:
template <int... Seq> struct sequence {}; template <int N, int... Seq> struct
make_seq : make_seq<N - 1, N - 1, Seq...> {}; template <int... Seq> struct
make_seq<0, Seq...> { using type = sequence<Seq...>; }; template <typename Func,
typename Tuple, int... Seq> decltype(auto) Apply_detail(const Func &func, const
Tuple&tu, const sequence<Seq...> &) { return func(std::get<Seq>(tu)...); // 参数展开
} template <typename Func, typename Tuple> decltype(auto) Apply(const Func &func
, const Tuple &tu) { return Apply_detail(func, tu, typename make_seq<std::
tuple_size<Tuple>::value>::type()); } void Show(int a, double b, const std::
string&c) { std::cout << a << " " << b << " " << c << std::endl; } int main(int
argc, const char *argv[]) { std::tuple tu(1, 2.5, "abc"); Apply(Show, tu);
return 0; }
这个seq的生成可能有点难懂,简单来说,假如我们tuple有4个参数,那么我们要把make_seq<4>变成make_seq<3, 3>,再变成
make_seq<2, 2, 3>,再变成make_seq<1, 1, 2, 3>,再变成make_seq<0, 0, 1, 2, 3>
,当第一个参数是0的时候,后面不再变化,这时候正好可以得到从0到3的序列,然后用这个参数序列构造出sequence<0, 1, 2, 3>
。然后,再利用这个序列,传给std::get来处理tuple。
折贴语句func(std::get<Seq>(tu)...)就变成了func(std::get<0>(tu), std::get<1>(tu),
std::get<2>(tu), std::get<3>(tu)),从而达成我们的目的。

而有了std::apply,它可以实现同样的功能,请看例子:
#include <iostream> int sum(int a, int b, int c) { return a + b + c; } int main
(int argc, const char *argv[]) { std::tuple tu(1, 2, 3); int res = std::apply(
sum, std::move(tu)); std::cout << res << std::endl; // 输出6 return 0; }
除了普通函数,apply的第一个参数还可以是函数指针、lambda、仿函数对象,不再赘述。
那么,对于非静态成员函数怎么办?很简单,把需要调用的对象作为tuple中的第一个成员即可,例如:
#include <iostream> struct TT { int sum(int a, int b, int c) { return a + b + c
; } }; int main(int argc, const char *argv[]) { std::tuple tu(TT(), 1, 2, 3);
// 第一个成员就是调用成员 int res = std::apply(&TT::sum, std::move(tu)); // 这里传成员函数指针 //
效果相当于std::get<0>(tu).sum(1, 2, 3) std::cout << res << std::endl; return 0; }
<>变参模板应用std::apply

那么,对于变参模板要怎么办?由于std::apply
的第一个参数是一个可调用对象,因此,对于模板来说,必须要进行实例化。而对于变参模板来说,它本身存在意义就在于可以根据参数来自动推导出模板的类型,如果要手动实例化的话,就会失去意义,请看例程:
#include <iostream> template <typename... T> void test(T... arg) { (std::cout
<< ... << arg) << std::endl; } int main(int argc, const char *argv[]) { std::
tupletu(1, 2.5, "abc"); std::apply(test, std::move(tu)); // err std::apply(test<
int, double, const char *>, std::move(tu)); // 没意义 return 0; }
怎么办呢?其实,可以简单用lambda封装一下就好了,把变参目标的调用放在lambda当中,然后把类型推导的任务交给lambda本身即可,也就是auto
大法好啦。请看例程:
#include <iostream> template <typename... T> void test(T... arg) { (std::cout
<< ... << arg) << std::endl; } int main(int argc, const char *argv[]) { std::
tupletu(1, 2.5, "abc"); std::apply([](auto &&... args) {return test(args...);},
std::move(tu)); // 正常调用 return 0; }
<>std::make_from_tuple

std::apply可以解决函数调用时,tuple转参数列表,但是如果希望调用的是构造函数怎么办?构造函数毕竟没办法直接获取函数指针。
其中一种办法就是自行封装一层模板,例如:
class Test { public: Test(int a, double b, const std::string &c): a_(a), b_(b),
c_(c) {} void show() const {std::cout << a_ << " " << b_ << " " << c_ << std::
endl;} private: int a_; double b_; std::string c_; }; // 自行封装构造过程 template <
typename T, typename... Args> T Create(Args &&...args) { return T(args...); }
int main(int argc, const char *argv[]) { std::tuple tu(1, 2.5, "abc"); Test &&t
= std::apply([](auto &&...args)->Test {return Create<Test>(args...);}, std::move
(tu)); t.show(); // 打印:1 2.5 abc return 0; }
而STL提供的std::make_from_tuple就是同样的作用,我们可以直接用它来代替自行实现的Create函数:
#include <iostream> // Test实现同上,省略 int main(int argc, const char *argv[]) { std
::tuple tu(1, 2.5, "abc"); Test &&t = std::make_from_tuple<Test>(std::move(tu));
t.show(); return 0; }

技术
今日推荐
PPT
阅读数 131
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信