Boost logo

Boost :

Subject: Re: [boost] Boost.Exception and constexpr
From: Peter Dimov (lists_at_[hidden])
Date: 2013-01-10 08:24:06


> On second thought, this perhaps does make it possible to have something
> assert-like in a constexpr function.
>
> constexpr const_reference operator[]( size_type i ) const
> {
> return i < size()? elems[i]: ( abort(), elems[i] );
> }

I asked on c++std-lib and Nikolay Ivchenkov suggested:

#define CONSTEXPR_CHECKER(condition, expr, failure_handler) \
    ((condition) ? expr : ((failure_handler), void(), expr))

and then

    CONSTEXPR_CHECKER( i < size(), elems[i], assert(i < size()) );

I think that there's no point in repeating the expression, so maybe

#define BOOST_CONSTEXPR_ASSERT(cond, expr) \
    ((cond)? expr: (BOOST_ASSERT(expr), expr))

constexpr const_reference operator[]( size_type i ) const
{
    return BOOST_CONSTEXPR_ASSERT( i < size(), elems[i] );
}

Similarly for _MSG. Or even

#define BOOST_CONSTEXPR_ASSERT(cond, expr) \
    ((cond)? expr: (BOOST_ASSERT_MSG(expr, #expr " requires " #cond), expr))


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