Boost logo

Boost Users :

From: Zara (yozara_at_[hidden])
Date: 2005-12-20 03:46:50


[Note to moderator]

I am not surte if previous version of this message will really go
through, beacuase I was not yet subscribed to users list.

Should that be the case, please forgive me.

[End of note to moderator]

Dear all,

I have been doing a deep redesign of an old SW project, and in the
course of it I have changed a typical struct {enum;union;} to
boost:variant.

This design change has surfaced some problems of the original SW, as
there were lots of implicit/explicit casting when accesing the above
mentioned struct.

To detect and correct all this problems, I have found that boost::get
working on boost:variant is not enough, it will only give either a
null pointer or a bad_get exception when trying to access a variant
with the wrong type. Even in the cases that the wrong type is
unaccessible all the time, as it is not included in the variant list
of types. IMHO, this should be detected at compile-time, as it denotes
a design flaw, not a runtime access error.

To cover this cases, I suggest the following addition to variant
library, based on get definition:

namespace boost {

template <typename U, BOOST_VARIANT_ENUM_PARAMS(typename T) >
inline
    typename add_pointer<U>::type
checked_get(
      boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) >* operand
      BOOST_VARIANT_AUX_GET_EXPLICIT_TEMPLATE_TYPE(U)
    )
{
    typedef boost::variant< BOOST_VARIANT_ENUM_PARAMS(T) > Variant;
    BOOST_STATIC_ASSERT((!is_same<
        typename mpl::find<typename Variant::types,U>::type,
        typename mpl::end<typename Variant::types>::type>::value));

    typedef typename add_pointer<U>::type U_ptr;
    if (!operand) return static_cast<U_ptr>(0);

    detail::variant::get_visitor<U> v;
    return operand->apply_visitor(v);
}

} // end of addition to namespace boost

And, of course, the rest of the templates needed for const *, ref and
const&..

Best regards,

-- Zara


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net