Re:Functional Programming in C++
by el_presidente on Friday April 27, 2012 @12:58AM
(#3897618)
GCC has a 'pure' attribute. I don't know if it's only used for optimization, but it also serves as a kind of comment for whoever looks at the function in the future.
[ Parent
| on HN ]
"pure" and "const" are GCC function attributes which are supposed to help the optimizer. Confusingly enough "const" demands a higher level of purity than "pure"..
I would never use those attributes except - maybe - in the most critical loops and then only with a big "WARNING!!!" sign. Why? GCC does not / cannot enforce the guarantees given by "const" and "pure" yet the optimizer always assumes they are true. I.e. accidentally reduce the purity of a function decorated in this way.. and you might create very hard to find bugs.
This is a general problem in C and C++, those languages simply weren't designed to enforce such contracts. C99's "restrict" has the same problem.
Since when is "const" a GCC function attribute?
http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
This is, of course, in addition to its other meanings in the language.