https://gcc.gnu.org/bugzilla/show_bug.cgi?id=18487
Federico Kircheis <federico.kircheis at gmail dot com> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |federico.kircheis at gmail dot com --- Comment #21 from Federico Kircheis <federico.kircheis at gmail dot com> --- FWIW I was going to open a similar ticket that there are no diagnostic for [[gnu::const]] and [[gnu::pure]] used incorrectly, so I would like to add my opinion. I think it would be very valuable when the attribute is used correctly, as it can help the compiler optimize the code, but more importantly help the reader reason about it (and verify the correctness of a program). Currently those attributes are problematic when maintaining code. If for example a [[gnu::pure]] function gets refactored and it's not pure anymore, then the code might misbehave, as the compiler might optimize incorrectly, and tracking down the bug is not easy. For example, suppose we have ---- // bar.hpp [[gnu::const]] int get_value(); // bar.cpp int get_value(){ return 42;} // foo.cpp #include "bar.hpp" int foo(){ int i = get_value(); int j = get_value(); return i+j; } ---- The the compiler can and will optimize the second call of `get_value`. But if the code changes, and one forgets to change the declaration: ---- // bar.hpp [[gnu::const]] int get_value(); // bar.cpp int get_value(){static int i = 0; return ++i;} // foo.cpp #include "bar.hpp" int i = get_value(); int j = get_value(); return i+j; } ---- The compiler will still optimize the call to get_value, (unless it is able to see the definition of get_value and see that there are side effects). I think a warning that even only works for trivial case is much better than nothing, because at least I know I can safely use the attribute for some functions as a contract to the caller, and have it checked. If the compiler has false positives I can at least recheck the offending functions and evaluate if leaving the attribute, or removing it.