Debian Codegraph @

yo/classtemplates/examples/tuples.cc

raw
1 #include <tuple>
2 #include <iostream>
3 #include <algorithm>
4
5 #include <bobcat/typetrait>
6
7 template <typename ...Types>
8 struct Multi
9 {
10 std::tuple<Types...> d_tup;
11
12 Multi(Types &&...types)
13 :
14 d_tup(std::forward<Types>(types)...)
15 {}
16 };
17
18 template <typename Result, typename Opnd, typename ...Cntxt>
19 struct Wrapper
20 {
21 mutable std::tuple<Cntxt...> d_cntxt;
22
23 Result (*d_fun)(Opnd, std::tuple<Cntxt...>& );
24
25 Wrapper(Result fun(Opnd, std::tuple<Cntxt...> &), Cntxt &&...cntxt)
26 :
27 d_cntxt(std::forward<Cntxt>(cntxt)...),
28 d_fun(fun)
29 {}
30
31 Result operator()(Opnd &&opnd) const
32 {
33 return d_fun(std::forward<Opnd>(opnd), d_cntxt);
34 }
35 };
36
37
38 using namespace std;
39
40
41 void handle(std::string const &str, tuple<size_t &, ostream &> &tup)
42 {
43 ++get<0>(tup);
44 get<1>(tup) << "Saw: " << str << '\n';
45 }
46
47 int main(int argc, char **argv)
48 {
49 Multi<int, double, int> multi{ 3, 12.5, 5 };
50
51 size_t count = 0;
52
53 for_each(argv, argv + argc, Wrapper<void, std::string const &,
54 size_t &, ostream &>(handle, count, cout));
55
56 cout << "========> " << count << '\n';
57 cout << get<1>(multi.d_tup) << '\n';
58 }