Boost logo

Boost :

Subject: Re: [boost] [variant]compile-time-checked boost::get<T>?
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-12-10 21:27:21


AMDG

Joe Gottman wrote:
> Mathias Gaunard wrote:
>> Arno Schödl wrote:
>>
>>> As a further extension, boost::get<S> where S is a base class of a T
>>> contained in the variant, should also work. I believe this would
>>> also be implementable with enable_if/disable_if, but we have not
>>> done it, so no guarantee.
>>
>> Actually, it should work with all types that are convertible from any
>> of the types of the variant.
>>
>
> Would it work for a type that is convertible from two or more of
> the types of the variant?

It had better not compile in that case, unless one of the types is a
better match
than the rest.

How about implementing get along these lines (untested):

template<class T, class Variant, class Base>
struct get_impl : Base {
    operator typename mpl::at_c<typename Variant::types>::type&() const {
        return(get<T>(*this->variant));
    }
};

template<class Variant>
struct get_impl_base {
    Variant* variant;
};

template<class T, class Variant>
T checked_get(const Variant& variant) {
    typename mpl::fold<typename Variant::types, get_impl_base<Variant>,
get_impl<_1, Variant, _2> >::type impl;
    impl.variant = &variant;
    return T(impl);
}

In Christ,
Steven Watanabe


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