Since you are using boost::variant , what is wrong with using its recursive wrappers?
You can see a short example in the tutorial :
typedef boost::make_recursive_variant< int , std::vector< boost::recursive_variant_ > >::type int_tree_t; std::vector< int_tree_t > subresult; subresult.push_back(3); subresult.push_back(5); std::vector< int_tree_t > result; result.push_back(1); result.push_back(subresult); result.push_back(7); int_tree_t var(result);
And it works as expected.
source share