zirias@
Developer
Consider a function like this:
Semantics: Get some named (
Many compilers support a "pure" attribute. Is it safe to mark this function as pure? Looking for example in GCC's documentation, I find this:
const char *getSomeValue(const struct some_container *cont, const char *key, size_t *len);
Semantics: Get some named (
key
) value from some container, optionally also return its length in len
. Real-life example why this optional output parameter makes sense would be some utf-8 encoded string with len
counting characters instead of octets.Many compilers support a "pure" attribute. Is it safe to mark this function as pure? Looking for example in GCC's documentation, I find this:
Well, not really the case here ? ... but this paragraph describing the purpose of the attribute:The pure attribute prohibits a function from modifying the state of the program that is observable by means other than inspecting the function’s return value.
looks like a match to me. The function clearly is idempotent. Do I just have some edge-case here that wasn't considered writing this GCC documentation?Calls to functions that have no observable effects on the state of the program other than to return a value may lend themselves to optimizations such as common subexpression elimination. Declaring such functions with the pure attribute allows GCC to avoid emitting some calls in repeated invocations of the function with the same argument values.