#include #include #include #include typedef boost::make_recursive_variant< int, std::map >::type element; // Use this typedef as a stand-in for the second type in your variant typedef std::map map_t; struct printer : public boost::static_visitor<> { template void operator () (const T& t) const { m_os << t; } void operator () (const map_t& m) const { m_os << '{'; typedef map_t::const_iterator iterator; for (iterator i = m.begin (), e = m.end (); i != e; ++i) { if (i != m.begin()) m_os << ", "; m_os << '(' << i->first << ", "; boost::apply_visitor (*this, i->second); m_os << ')'; } m_os << '}'; } printer (std::ostream& os) : m_os (os) {} ~printer () { m_os << std::endl; } private: mutable std::ostream& m_os; }; int main () { element i = 42; map_t the_map; the_map.insert (std::make_pair (1.23f, i)); the_map.insert (std::make_pair (3.14159f, std::numeric_limits::max())); the_map.insert (std::make_pair (2.46f, the_map)); printer (std::cout).operator() (the_map); return 0; }