Boost logo

Boost Users :

From: Caleb Epstein (caleb.epstein_at_[hidden])
Date: 2004-08-24 13:08:33

On Mon, 23 Aug 2004 08:42:09 -0700, Steven L. Scott <sls_at_[hidden]> wrote:

> I'm working on an application (a data analysis system built in C++) for
> which it would be helpful to have nested (multidimensional) containers
> other than ordinary arrays.

I've been toying with using boost::variant for something like this -
an arbitrarily nested data structure that could be used like the
hashes in Perl/Python etc. Unfortunately, because boost::variant
doesn't know about the std::map operators, you can't use code like
thedata["first"]["second"] but you can write something like this
(data_t is used just to gain the syntactic sugar of

#include <map>
#include <string>
#include <boost/variant.hpp>

typedef boost::make_recursive_variant<
    std::map<std::string, boost::recursive_variant_>
>::type atom_t;

typedef std::map<std::string, atom_t> data_t;

int main ()
    data_t scores, student_scores;

    student_scores["Midterm"] = 90.0;
    student_scores["Final"] = 88.0;
    scores["johnny"] = student_scores;

I'd love to get some feedback on this approach, especially if anyone
has a solution for implementing data["foo"]["bar"] etc.

Caleb Epstein

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at