John Porter <[EMAIL PROTECTED]> writes:
> Russ Allbery wrote:

>> It looks like I was misremembering; I remember a proposal for a "pure"
>> attribute in gcc, but it looks like the attribute used for functions
>> with no memory references and no side effects is "const" (a la C++).  I
>> think "pure" was proposed for the somewhat relaxed version of that that
>> allowed memory references but not side effects.

> Are you sure?  That sounds totally backwards to me.  Declaring a
> function "const" is a promise that it's not going to change anything
> outside its call frame.  But a pure function, in the math sense, doesn't
> even look at anything outside its call frame.

I'm sure about the definition of const:

`const'
     Many functions do not examine any values except their arguments,
     and have no effects except the return value.  Such a function can
     be subject to common subexpression elimination and loop
     optimization just as an arithmetic operator would be.  These
     functions should be declared with the attribute `const'.  For
     example,

          int square (int) __attribute__ ((const));

     says that the hypothetical function `square' is safe to call fewer
     times than the program says.

     The attribute `const' is not implemented in GNU C versions earlier
     than 2.5.  An alternative way to declare that a function has no
     side effects, which works in the current version and in some older
     versions, is as follows:

          typedef int intfn ();
          
          extern const intfn square;

     This approach does not work in GNU C++ from 2.6.0 on, since the
     language specifies that the `const' must be attached to the return
     value.

     Note that a function that has pointer arguments and examines the
     data pointed to must *not* be declared `const'.  Likewise, a
     function that calls a non-`const' function usually must not be
     `const'.  It does not make sense for a `const' function to return
     `void'.

My memory of "pure" is from mailing list traffic that I don't have on
hand.

-- 
Russ Allbery ([EMAIL PROTECTED])             <http://www.eyrie.org/~eagle/>

Reply via email to