I have this code:
#include <stdio.h>
class A
{
public:
int doit()
{
return 5;
}
int doit2() const
{
i++;
return i;
}
int i;
};
int main()
{
A a;
printf("%d\n", a.doit() );
return 0;
}
Which compiles cleanly with g++ -Wall -Wpedantic main.cpp. Is there some way to get g++ to say "A::doit() should be marked as const"? g++ 4.8 has -Wsuggest-attribute=const but it doesn't seem to work in this case. g++ -Wall -Wpedantic -Wsuggest-attribute=const const_main.cpp -fipa-pure-const -O2 -Wextra is still clean.
I agree that const is a design decision but what I'm dealing with is a case of many lines of legacy code and for new developers coming in it would be helpful if const functions were marked as such. I think the compiler knows enough because if I mark a function as const and then modify state it will throw an error. I'm just asking for the opposite of that so that I can rip through and mark const functions as const and I don't even need it to be perfect and figure out really complicated cases, I would settle for the simple cases as I have outlined in the code above.
Now I added a non-const function doit2() but declared it const and the compiler says:
const_main.cpp: In member function ‘int A::doit2() const’:
const_main.cpp:12:6: error: increment of member ‘A::i’ in read-only object
i++;
^
I just need the opposite of that ( tell me when it could be const but it isn't marked as such ).
Found the answer over here: Const correctness warnings c++