https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80711
Bug ID: 80711 Summary: warn on non-const accessor member functions Product: gcc Version: 7.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- C++ accessor (and similar) member functions that return a value computed from one or more private data members without modifying the owning object can and should be declared const. Doing so not only improves the const-correctness of code that relies on objects of the type, it also aids its analyzability. It's easy (and not uncommon) to forget to declare accessors const. GCC could help detect the missing const by issuing a warning on such accessors, similarly to how it helps detect candidates for attribute const and pure. This is an enhancement to request to add such a warning. The test case below illustrates where it would be issued and shows the similar -Wsuggest-attribute warning that the new one could be modeled on. $ cat t.C && gcc -O2 -S -Wall -Wextra -Wsuggest-attribute=pure t.C class Int { public: int get () { return val; } // suggested warning: function can be declared const private: int val; }; int get_int (Int &i) { return i.get (); } t.C: In function ‘int get_int(Int&)’: t.C:10:5: warning: function might be candidate for attribute ‘pure’ [-Wsuggest-attribute=pure] int get_int (Int &i) ^~~~~~~