|
|
Subscribe / Log in / New account

Constant function declaration: is it cheating?

Constant function declaration: is it cheating?

Posted Jun 14, 2008 16:34 UTC (Sat) by giraffedata (guest, #1954)
In reply to: Constant function declaration: is it cheating? by jimparis
Parent article: Implications of pure and constant functions

If you can look at a function, and the way it behaves in your program, and guarantee "at any time in my program, during proper usage, this function will always return the same answer" then sure, call it constant, and you're not cheating or lying to the compiler, you're just being 100% accurate and using the attribute properly.

That's all I'm suggesting. The "constant" attribute must always mean the function is constant, not that you want the compiler to treat it as if it were constant. (It's none of your business what the compiler does with the information).

The only question is the definition of constant. The article's use of the term "cheating" suggests to me a definition of constant that is exactly what the compiler would determine on its own, e.g. if the function accesses global memory, it's not constant. But the examples given are of cases where the function really is constant under the broader definition.


to post comments


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds