checkedint call removal

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Wed Jul 30 14:21:09 PDT 2014


On 07/30/2014 10:39 PM, Walter Bright wrote:
> On 7/30/2014 2:12 AM, bearophile wrote:
>> Info about assume in Microsoft C++:
>> http://msdn.microsoft.com/en-us/library/1b3fsfxw.aspx
>
> Note that document shows how assert

Some random ASSERT macro with custom behaviour. That article shows a few 
alternatives for an ASSERT macro, one of which is basically what D's 
assert expression does.

The primitives used are

( (e) || assert(__FILE__, __LINE__))

and

__assume(e);

Because the compiler does not seem know that 'assert' never returns, 
they shove in an __assume after it.

#define ASSERT(e)    ( ((e) || assert(__FILE__, __LINE__)), __assume(e))

This is an implementation detail.

One can just as well do e.g.:

#ifdef DEBUG
# define ASSERT(e)    ( ((e) || assert(__FILE__, __LINE__) )
#else
# define ASSERT(e)
#endif

#define ASSUME(e) __assume(e)

and this and similar options avoid undefined behaviour in non-DEBUG mode 
caused on ASSERT, while still having the possibility of giving 
optimization hints explicitly in the form of ASSUME. In this case, the 
distinction is important even operationally, and it would be a sane 
alternative to the current approach. There is no a priori absolute 
single true way when discussing alternatives, and frankly, this should 
be obvious.

> is implemented using assume. I.e. they are the same thing.
> ...

Yes, and writeln is obviously the same thing as a hello world program.

> Intriguingly, the __assume(0) behavior is even special cased like D's
> assert(0). ...

It is not special cased at all. It is documented specially, because not 
everyone seems to be that deep into logic.


More information about the Digitalmars-d mailing list