On Friday, 4 March 2016 23:09:40 UTC+2, Ian Collins wrote:
> On 03/05/16 03:02, Öö Tiib wrote:
> >
> > Calling non-constexpr from constexpr function (like
> > that 'compileTimeCheckFailed') and/or throwing from
> > constexpr function without using ?: operator should not
> > compile AFAIK. Maybe something like that:
> >
> > constexpr int f(int n)
> > {
> > (n < 1000) ? true : throw 42;
> > // ... do what you need
> > return 0;
> > }
>
> That is basically the fiddle I came up with. The compiler errors are a
> little obscure, but a comment fixes that. I added This:
>
> constexpr int checkRange( int n ) {
> (n < 1000) ? true : throw 0; return n; }
More often people merge there like:
constexpr int checkRange( int n ) {return (n < 1000) ? n : throw 0;}
>
> constexpr int f( int n ) { checkRange( n ); return n; }
>
> It looks like the latest g++ has trouble spotting this in some
> conditions, for example given these two lines:
>
> constexpr int x {f(1000)};
> char buf[f(8000)];
>
> g++ >=5 spots the first as an error, but not the second. g++ 4.9 just
> spits the dummy and runs.
The g++ has some sort of variable-length automatic arrays extension
so it somehow weasels out using that on second case I suspect.
>
> Oracle CC spots them both:
>
> Error: Initializer for constexpr variable x must be a constant expression.
> Error: An integer constant expression is required within the array
> subscript operator.
Yes, compiler makers are humans too and sometimes mix up when it
is "ill-formed" so they have to diagnose and when it is "undefined
behavior" so they may screw us.
The constexpr is relatively new so we have to calmly fire the bugs
for some years.