File: tuples.cc

package info (click to toggle)
c%2B%2B-annotations 13.02.02-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,576 kB
  • sloc: cpp: 25,297; makefile: 1,523; ansic: 165; sh: 126; perl: 90; fortran: 27
file content (58 lines) | stat: -rw-r--r-- 1,191 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <tuple>
#include <iostream>
#include <algorithm>

#include <bobcat/typetrait>

template <typename ...Types>
struct Multi
{
    std::tuple<Types...> d_tup;

    Multi(Types &&...types)
    :
        d_tup(std::forward<Types>(types)...)
    {}
};

template <typename Result, typename Opnd, typename ...Cntxt>
struct Wrapper
{
    mutable std::tuple<Cntxt...> d_cntxt;

    Result (*d_fun)(Opnd, std::tuple<Cntxt...>& );

    Wrapper(Result fun(Opnd, std::tuple<Cntxt...> &), Cntxt &&...cntxt)
    :
        d_cntxt(std::forward<Cntxt>(cntxt)...),
        d_fun(fun)
    {}

    Result operator()(Opnd &&opnd) const
    {
        return d_fun(std::forward<Opnd>(opnd), d_cntxt);
    }
};


using namespace std;


void handle(std::string const &str, tuple<size_t &, ostream &> &tup)
{
    ++get<0>(tup);
    get<1>(tup) << "Saw: " << str << '\n';
}

int main(int argc, char **argv)
{
    Multi<int, double, int> multi{ 3, 12.5, 5 };

    size_t count = 0;

    for_each(argv, argv + argc, Wrapper<void, std::string const &,
                               size_t &, ostream &>(handle, count, cout));

    cout << "========> " << count << '\n';
    cout << get<1>(multi.d_tup) << '\n';
}