On 12/09/2011 04:37 PM, Regan Heath wrote:
On Fri, 09 Dec 2011 14:54:16 -0000, Timon Gehr <timon.g...@gmx.ch> wrote:
10)
return
c <= 0x7F ? 1
: c <= 0x7FF ? 2
: c <= 0xFFFF ? 3
: c <= 0x10FFFF ? 4
: (assert(false), 6);
*Much* clearer with the rewrite..
assert(c <= 0x10FFFF);
return
c <= 0x7F ? 1
: c <= 0x7FF ? 2
: c <= 0xFFFF ? 3
: c <= 0x10FFFF ? 4
: 6;
This is a *much* better rewrite:
assert(c <= 0x10FFFF);
return
c <= 0x7F ? 1
: c <= 0x7FF ? 2
: c <= 0xFFFF ? 3
: 4;
Again, I was missing context.. is the return value of 6 not
required in
release mode?
I was joking here. Your 'rewrite' of the original example changed its
release mode semantics, therefore I did the same thing.
My version performs the assert in all cases, throws an assert error in
the same/error cases, and still returns the correct/original values. The
only change is performing the assert in all cases, so I don't see how
that is a problem. Yours however is entirely broken (assuming the return
value of 6 is desired/required).
R
What you might be missing is that assert(false) is not compiled out in
release mode. It emits a 'hlt' instruction which kills your program.
However, your assert(c <= 0x10FFFF); will be removed in release mode.
I was indeed missing that. I couldn't find anything about it on the
website. :)
R
http://d-programming-language.org/expression.html#AssertExpression