Boost logo

Boost :

Subject: Re: [boost] Interest in a container which can hold multiple data types?
From: James Armstrong (armstrhu_at_[hidden])
Date: 2015-05-10 13:05:51


On Wed, May 6, 2015 at 5:37 AM, Boris Rasin <boris_at_[hidden]> wrote:

> boost::variant (unlike boost::any) stores data by value.
>
> As to what "feels cleaner": a single algorithm function which accepts any
> sequence container (std::vector, std::array, std::list, std::forward_list,
> boost::circular_buffer, etc.) with any type erased or discriminated union
> objects (boost::any, boost::type_erasure::any, boost::variant,
> eggs::variant, etc.) or a single specialized container type mimicking
> behavior of vector<any>?

So, I went ahead and modified the code to be templated over a sequence
container. You can see the code here,

https://github.com/armstrhu/poly_adaptor

poly_adaptor.h defined the class and main.cpp has some code with use
examples.

So, one issue is that the type erased types do not have the same methods to
access their data. For instance, boost::any requires that you use a
boost::any_cast<T> in order for you to get the value, whereas a
boost::variant uses the get<T> function or you can access it with a
stream. Basically, it will be hard to have a nice templated class for
these, others, and newly developed type erased types without a common
method to access the data.

The other thing, as you mentioned above, is that this will have to be
limited to sequence containers. It would be nice to come up with something
fully general, but it again comes down to methods to access data. For
example, the method for getting data from an std::map is very different
than from a sequence container.

Therefore, right now it seems this will be limited to sequence containers
of boost::any only. Thoughts? Any ideas for a clean way around any of
these?

-- 
James Armstrong

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk