-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrecursive_test.cpp
More file actions
56 lines (44 loc) · 1.19 KB
/
recursive_test.cpp
File metadata and controls
56 lines (44 loc) · 1.19 KB
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
#include "boost/variant.hpp"
#include <iostream>
#include <sstream>
#include <vector>
#include <boost/cstdlib.hpp>
struct vector_printer
: boost::static_visitor<std::string>
{
template <typename T>
auto operator()(const std::vector<T>& vec) const -> std::string
{
std::ostringstream ost;
ost << "( ";
typename std::vector<T>::const_iterator it = vec.begin();
for (; it != vec.end(); ++it)
ost << boost::apply_visitor(vector_printer(), *it);
ost << ") ";
return ost.str();
}
template <typename T>
auto operator()(const T& operand) const -> std::string
{
std::ostringstream ost;
ost << operand << ' ';
return ost.str();
}
};
int main(int , char* [])
{
typedef boost::make_recursive_variant<
int,
std::vector<boost::recursive_variant_>
>::type var_t;
std::vector<var_t> vec;
vec.push_back(3);
vec.push_back(5);
vec.push_back(vec);
vec.push_back(7);
var_t var(vec);
std::string result( boost::apply_visitor( vector_printer(), var ) );
std::cout << "result: " << result << '\n';
// BOOST_CHECK(result == "( 3 5 ( 3 5 ) 7 ) ");
return boost::exit_success;
}