Subject: Re: [Boost-bugs] [Boost C++ Libraries] #547: [variant] Compile time checked getter
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2014-12-10 13:44:00
#547: [variant] Compile time checked getter
-------------------------------+--------------------------
Reporter: nobody | Owner: apolukhin
Type: Feature Requests | Status: new
Milestone: Boost 1.58.0 | Component: variant
Version: None | Severity: Optimization
Resolution: None | Keywords:
-------------------------------+--------------------------
Changes (by apolukhin):
* owner: ebf => apolukhin
* status: assigned => new
* milestone: => Boost 1.58.0
Old description:
> {{{
> [Sorry if anyone feels bad about this, I already posted
> it in usera and devel lists, with no answer at all]
>
> During a redesign, I began using boost::variant, and I
> detected that trying to get a value of a type not
> included in variant type-list resulted in run-time
> error (cast or null pointer).
>
> I feel that a variant should only try to return a valid
> type from its type-list, otherwise giving a compiler
> error, and reserve runttime errors for that case when
> someone tries to get a value from a type contained in
> the typelist, but not the actual type contained in the
> actual object.
>
> In such a way, I have made a little redesign on
> boost::get and called it boost::checked_get:
>
> 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;
>
> /* throw a compile-time error if type is not
> contained on type
> list */
> 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&..
>
> I think it would be interesting to include it in boost
> libraries. In fact, I use it intensely.
>
> Best regards,
>
> Zara (yozara_at_[hidden])
> }}}
New description:
{{{
[Sorry if anyone feels bad about this, I already posted
it in usera and devel lists, with no answer at all]
During a redesign, I began using boost::variant, and I
detected that trying to get a value of a type not
included in variant type-list resulted in run-time
error (cast or null pointer).
I feel that a variant should only try to return a valid
type from its type-list, otherwise giving a compiler
error, and reserve runttime errors for that case when
someone tries to get a value from a type contained in
the typelist, but not the actual type contained in the
actual object.
In such a way, I have made a little redesign on
boost::get and called it boost::checked_get:
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;
/* throw a compile-time error if type is not
contained on type
list */
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&..
I think it would be interesting to include it in boost
libraries. In fact, I use it intensely.
Best regards,
Zara (yozara_at_[hidden])
}}}
-- Comment: Series of commits were merged to develop branch to fix this issue. At this moment last commit is [https://github.com/boostorg/variant/commit/cbf4bbf1b1df5c3bbc65fec1f2de3351c141cfd3 cbf4bbf] -- Ticket URL: <https://svn.boost.org/trac/boost/ticket/547#comment:7> Boost C++ Libraries <http://www.boost.org/> Boost provides free peer-reviewed portable C++ source libraries.
This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:17 UTC