[Bug c++/80711] warn on non-const accessor member functions

msebor at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Sat May 9 00:05:54 GMT 2020


--- Comment #6 from Martin Sebor <msebor at gcc dot gnu.org> ---
You're right, there is a substantial difference between attributes const/pure
and constness in the C/C++ sense.  A warning that detects missing const on
member functions (i.e., this request) is implementable in the C++ front end,
while one that also considers accesses to the state of the object would have to
be implemented in the middle end (like -Wsuggest-attribute).  Only there is it
possible to tell if a pointer dereference accesses *this.

As an aside, a risk with enabling -Wsuggest-attribute= is that GCC doesn't
detect accesses that are invalid in pure functions (like to globals), or any
other misuses in functions declared with any of the suggested attributes. 
Since what is and isn't safe isn't always clear to everyone, it's easy to get
it wrong.  Once the detection is implemented, hopefully for GCC 11 (pr18487),
using attributes pure and const will be a lot safer.  But that's independent of
improving const-correctness.

More information about the Gcc-bugs mailing list