4 ms·
>__attribute__((const)) which tells it that the func accesses NOTHING but the params, not even global vars. If you're actually not accessing any global state a
by pingyong 7y ago
>__attribute__((const)) which tells it that the func accesses NOTHING but the params, not even global vars.
If you're actually not accessing any global state at all (including making no memory allocation), shouldn't the compiler figure that out anyway? That doesn't seem like something that would be very hard to check for.
- gpderetta 7y agoNot if it is defined in another translation unit and you are not using LTO.
- fluffything 7y agoIn other words, the compiler can infer this from the function definition (or at least clang does), but it cannot infer this from a function declaration, because the implementation is missing - unless you are using LTO like the parent mentions.
- dmitrygr 7y agosometimes it cannot infer it even from function, for example, here is a function that in my particular OS gets me a pointer to my library's globals. To my code the returned pointer never changes, but the compiler has NO way to tell this function is const without my annotation void* __attribute__((const)) getGlobals(void) { void* ret; asm ( " ldr %0, [r9] \n" " ldr %0, [%0, %1] \n" : "=r"(ret) : "I"(MY_MODULE_ID) : ); return ret; }
- gpderetta 7y agoGood point.