Boost logo

Boost :

Subject: Re: [boost] [variant]compile-time-checked boost::get<T>?
From: Arno Schödl (aschoedl_at_[hidden])
Date: 2008-12-11 04:05:11

> > 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.

Arguably, only conversion to an L-value are o.k., do you agree? But boost::is_convertible<T const&, S const&> compiles on MSVC9 even if the conversion is to an R-value:

class S {};

class T {
        operator S() const{
                return S(); // returns a value!

// but cast to const reference works
T t;
static_cast<S const&>(t);

// and is_convertible says it does
BOOST_STATIC_ASSERT( boost::is_convertible<T const& BOOST_PP_COMMA() S const&>::value );

I think we must require boost::is_convertible<T &, S &> and also make sure static_cast<S &>(T) is actually used. The cast inside boost::get for a constant variant would then look like this:

static_cast<S const&>( static_cast<S &>( const_cast<T &>( t ) ) );

I am not sure whether this always does the intended thing. The problem to ensure that a cast is to L-value seems quite general, so maybe someone knows a standard solution?

P.S. Did I get the L-value vs. R-value terminology right :-) ?

Dr. Arno Schoedl · aschoedl_at_[hidden] 
Technical Director 
think-cell Software GmbH · Invalidenstr. 34 · 10115 Berlin, Germany · phone +49-30-666473-10 · toll-free (US) +1-800-891-8091
Directors: Dr. Markus Hannebauer, Dr. Arno Schoedl · Amtsgericht Charlottenburg, HRB 85229

Boost list run by bdawes at, gregod at, cpdaniel at, john at