Boost logo

Boost :

Subject: Re: [boost] Boost.Exception and constexpr
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2013-01-10 08:49:36

On Thu, Jan 10, 2013 at 3:47 PM, Peter Dimov <lists_at_[hidden]> wrote:
> Andrey Semashev wrote:
>> I don't think that the ternary operator and a throw expression is a
>> constant expression that can be used in a constexpr function. In
>> 5.19/2, the throw expression is explicitly mentioned as the one that
>> cannot make a constant expression.
> I suspect that you've missed the
> "but subexpressions of logical AND (5.14), logical OR (5.15), and
> conditional
> (5.16) operations that are not evaluated are not considered"
> part. You can have a throw expression, as long as it's not evaluated.

That's right, I didn't miss it. But when throw is evaluated the code
becomes malformed, doesn't it? Well, it probably should not compile
anyway (when, e.g. at() index is out of bounds) but not because at()
suddenly becomes invalid.

>> In fact, I cannot see how a constant expression (such as a constexpr
>> function) can throw.
> A constexpr function can throw when it's called with an argument that is not
> a constant expression.

I must be missing something crucial. My understanding was that
constexpr functions should be evaluated at compile time, am I wrong?

OTOH, I tried the following code with gcc 4.6.3 -std=gnu++0x:

template< typename T, unsigned int n >
struct array
    constexpr T at(unsigned int i) const
        return i < n ? T() : throw 0;

int main(int, char*[])
    array< int, 5 > arr;

and it compiled (crashing in runtime due to an uncaught exception). I'm lost.

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