
Hi, Sometimes we need to view a value as a variant and we don't want to copy the value into a variant. It'd be great if there's a variant::view that works like a variant but holds a reference to the value instead. The constructor should allow any reference to the types as in the variant, and a constructor that takes a reference to the variant, e.g. template<class T1, class T2...> class variant<T1, T2...>::view { view(T1&); view(T2&); ... view(variant&); }; And we can use apply_visitor/get on a view as on a variant. Thoughts? Thanks.

Am 19.06.2016 um 15:31 schrieb TONGARI J:
Hi,
Sometimes we need to view a value as a variant and we don't want to copy the value into a variant.
It'd be great if there's a variant::view that works like a variant but holds a reference to the value instead.
The constructor should allow any reference to the types as in the variant, and a constructor that takes a reference to the variant, e.g.
template<class T1, class T2...> class variant<T1, T2...>::view { view(T1&); view(T2&); ... view(variant&); };
And we can use apply_visitor/get on a view as on a variant.
Thoughts?
Why not boost::variant<T1*, T2*> ? I guess boost::variant<boost::none, T1&, T2&> would also work (boost::none, if it needs to be default-constructible) Or do you need to use the same visitors?
Thanks.
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

2016-06-19 21:54 GMT+08:00 Klemens Morgenstern <klemens.morgenstern@gmx.net> :
Am 19.06.2016 um 15:31 schrieb TONGARI J:
Hi,
Sometimes we need to view a value as a variant and we don't want to copy the value into a variant.
It'd be great if there's a variant::view that works like a variant but holds a reference to the value instead.
The constructor should allow any reference to the types as in the variant, and a constructor that takes a reference to the variant, e.g.
template<class T1, class T2...> class variant<T1, T2...>::view { view(T1&); view(T2&); ... view(variant&); };
And we can use apply_visitor/get on a view as on a variant.
Thoughts?
Why not boost::variant<T1*, T2*> ?
I guess boost::variant<boost::none, T1&, T2&> would also work (boost::none, if it needs to be default-constructible)
Or do you need to use the same visitors?
Same visitor. Thanks for reminding me, variant<T1&, T2&> is close to my intent, but probably suboptimal, I think it needs to visit the types to convert to variant<T1, T2>. For a view, it's just a discriminator assignment.
participants (2)
-
Klemens Morgenstern
-
TONGARI J