$include_dir="/home/hyper-archives/boost/include"; include("$include_dir/msg-header.inc") ?>
Subject: Re: [boost] Interest in a container which can hold multiple data types?
From: Boris Rasin (boris_at_[hidden])
Date: 2015-05-04 17:57:56
On 5/4/2015 9:50 PM, Thijs (M.A.) van den Berg wrote:
> I would actually prefer not to use type erasure: make a strong-typed and efficient heterogenous container. Something like this... ?
>
> template <typename... Args>
> struct tuple_vector : std::tuple< std::vector<Args>... >
> {
> typename std::tuple<Args...> type;
>
> template <typename T>
> void push_back(const T& value)
> { std::get< std::vector<T> >(*this).push_back(value); }
>
> template <typename T>
> void push_back( T&& value)
> { std::get< std::vector<T> >(*this).push_back(value); }
> };
>
>
> struct Point {
> double x;
> double y;
> };
> struct Line {
> double x;
> double y;
> double a;
> double l;
> };
> struct Rectangle {
> double x;
> double y;
> double a;
> double w;
> double h;
> };
> struct Circle {
> double x;
> double y;
> double r;
> };
>
> tuple_vector<Point, Line, Rectangle, Circle> shapes;
> shapes.push_back(Point{1.0, 1.0} );
> shapes.push_back(Point{1.0, 1.0} );
> shapes.push_back(Circle{1.0, 1.0, .3} );
> shapes.push_back(Rectangle{0.0, 0.0, 0.0, 1.0, 1.0} );
This is neat, but it' not quite the same thing. Unlike
std::vector<boost::any>, your tuple_vector does not organize objects
into strictly linear arrangement. Using your example, there is no way to
draw shapes in the order they were inserted into container.