Created
October 13, 2016 09:48
-
-
Save vittorioromeo/90953948078ed1b428188970bda2a85e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <boost/variant.hpp> | |
namespace impl | |
{ | |
struct vnum_wrapper; | |
using varr = std::vector<vnum_wrapper>; // <- indirection here | |
using vnum = boost::variant<int, varr>; | |
struct vnum_wrapper | |
{ | |
vnum _v; | |
template <typename... Ts> | |
vnum_wrapper(Ts&&... xs) : _v(std::forward<Ts>(xs)...) { } | |
}; | |
} | |
using vnum = impl::vnum; | |
using impl::varr; | |
struct vnum_printer | |
{ | |
void operator()(int x) const | |
{ | |
std::cout << x << "i\n"; | |
} | |
void operator()(const varr& arr) const | |
{ | |
for(const auto& x : arr) | |
{ | |
boost::apply_visitor(*this, x._v); | |
} | |
} | |
}; | |
int main() | |
{ | |
vnum v0 = varr{vnum{1}, vnum{2}, varr{vnum{3}, vnum{4}}}; | |
boost::apply_visitor(vnum_printer{}, v0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment