On Monday, 28 July 2014 at 15:52:23 UTC, John Colvin wrote:
On Monday, 28 July 2014 at 15:20:44 UTC, Ola Fosheim Grøstad wrote:
If asserts were used as optimization constraints

all available code is fair game as optimisation constraints. What you are asking for is a special case for `assert` such that the optimiser is blind to it.

bool foo(int a)
{
    //let's handwrite a simple assert
    if(a >= 0)
    {
        exit(EXIT_FAILURE);
    }
    //and then do something.
    return a < 0;
}

Of course the compiler is free to rewrite that as

bool foo(int a)
{
    if(a >= 0)
    {
        exit(EXIT_FAILURE);
    }
    return true;
}

Why should the situation be different if I use the builtin `assert` instead?

The problem is that in release mode, the asserts are removed. It would then be a very big mistake to still take them into account for optimizations, as if they were actually there.

To take your code:

    assert(a >= 0);
    return a < 0;

is equivalent to

    assert(a >= 0);
    return true;

but only in non-release mode. In release mode, this effectively becomes

    return a < 0;

which is _not_ equivalent to

    return true;

I believe this is was Ola is protesting about, and I agree with him. Such optimizations must only happen if the check stays.

Reply via email to