/
1_lambda.cpp
83 lines (68 loc) · 1.82 KB
/
1_lambda.cpp
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) 2015-2016 Vittorio Romeo
// License: Academic Free License ("AFL") v. 3.0
// AFL License page: http://opensource.org/licenses/AFL-3.0
// http://vittorioromeo.info | vittorio.romeo@outlook.com
// #define VR_USE_BOOST_VARIANT
#include <iostream>
#include <vector>
#include <type_traits>
#include <boost/hana.hpp>
#include "variant_aliases.hpp"
namespace impl
{
struct vnum_wrapper;
using varr = std::vector<vnum_wrapper>;
using vnum = vr::variant<int, float, double, varr>;
struct vnum_wrapper
{
vnum _data;
template <typename... Ts>
vnum_wrapper(Ts&&... xs)
: _data{FWD(xs)...}
{
}
};
}
using vnum = impl::vnum;
using impl::varr;
template <typename TReturn, typename... TFs>
auto make_recursive_visitor(TFs&&... fs)
{
return boost::hana::fix([&fs...](auto self, auto&& x) -> TReturn
{
return boost::hana::overload(FWD(fs)...)(
[&self](auto&& v)
{
return vr::visit_recursively(self, v);
},
FWD(x));
});
}
int main()
{
// clang-format off
auto vnp = make_recursive_visitor<void>
(
[](auto, int x) { std::cout << x << "i\n"; },
[](auto, float x) { std::cout << x << "f\n"; },
[](auto, double x) { std::cout << x << "d\n"; },
[](auto visit_self, const varr& arr)
{
for(const auto& x : arr)
{
visit_self(x);
}
}
);
// clang-format on
vnum v0{0};
vr::visit(vnp, v0);
v0 = 5.f;
vr::visit(vnp, v0);
v0 = 33.51;
vr::visit(vnp, v0);
v0 = varr{vnum{1}, vnum{2.0}, vnum{3.f}};
vr::visit(vnp, v0);
v0 = varr{vnum{5}, varr{vnum{7}, vnum{8.0}, vnum{9.}}, vnum{4.f}};
vr::visit(vnp, v0);
}