Boost logo

Boost Users :

Subject: Re: [Boost-users] help with boost::variant, accessing recursive types
From: Edward Diener (eldiener_at_[hidden])
Date: 2016-09-29 12:09:26


On 9/29/2016 1:08 AM, Littlefield, Tyler wrote:
> all,
> I have the following:
> boost::make_recursive_variant<int, bool, std::string,
> std::map<std::string, boost::recursive_variant_>>::type node;
> I'm populating this tree. I'm using libucl because it has a relaxed JSON
> syntax but I wanted to have a tree-like configuration here. The std::map
> is a key:value pair obviously. So my questions:

I am not an expert with variant, but give answers from reading the docs.

> How do I get std::maps from this variant?

typedef boost::make_recursive_variant<int, bool, std::string,
std::map<std::string, boost::recursive_variant_>>::type t_rvar;

t_rvar avar;

// do something with avar

std::map<std::string, t_rvar> & amap =
boost::get<std::map<std::string, t_rvar>>(avar);

seems like it should work.

> I know boost::get exists, will
> it just return a pointer to std::map?

Yes, if you pass it a pointer to your variant.

> does setting elements in the map
> set elements on the variant?

If you get a pointer or a reference to your map in the variant any
changes you make to the map is changing what is in the variant.

> I also didn't see any return info for what boost::get returns if the
> element does not exist. Will it just return null since it does return a T*?

Yes if you get a pointer. If you try to get a reference and it does not
exist it throws a bad_get exception.

> Finally, is there a way to see the type of the variant? I don't really
> care if it's a bool or string, although I would like to place some
> implicit checks on certain values when they're retrieved.

This I do not know.

>
> PS: If there's a way to pull this off with boost::property_tree, I would
> love to know how. I'm looking at using libucl as mentioned before
> because it provides a nginx-like not really JSON-style configuration syntax.
>
> Any tips would be appreciated. HOpefully these questions are clear and
> make sense.
>


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net