Boost logo

Boost :

From: Darren Cook (darren_at_[hidden])
Date: 2004-10-29 23:21:45


>>I'm hoping Thorsten Ottosen's contract programming will make it into
>>the standard so the compiler will understand asserts and be able to
>>optimize away the redundant ones.
>
> Compilers are already allowed to do that, and I predict no proposal
> that requires it to be done will ever be accepted into the standard.

"be able" falls somewhere between "allowed to" and "required to". An
optimizer will have a hard job analyzing all the code in a function so see
if any can be skipped. But if it can see a precondition is always satisfied
then it can jump directly to the body of the function and skip the precondition.

E.g.
   void f(int x){
   precondition{ x>=0 && x<=99; } //1
   ... //2
   }

   f(atoi(argv[2])); //Jumps to f, at 1
   f(34); //Jumps to f, at 2

   void g(int z){
   precondition{ z>=10 && z<50; }
   f(z*2); //Can also jump to f at 2, as range is 20..98
   }

I don't think Thorsten's proposal requires optimizations like this. But I
don't know enough about compiler writing to know if the above is ever likely
to be implemented.

Darren


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk