Visual Studio has many useful code analysis suggestions (I’m using VS2022). But sometimes I should not follow the suggestion and would like to suppress it, in certain places. As a contrived example:
class FooContainer
{
Foo* foo;
public:
const Foo* GetFoo() const { return foo; } //OK
Foo* GetFoo() { return foo; } //Suggestion: The member function can be made const
}
Here I don’t want to break const
correctness by allowing nonconst access to a Foo
through a const FooContainer
. It seems like the suggestion is here because the function does not modify the FooContainer
so, from the compiler’s perspective, it could be const
. But logically, this function should not be const
.
I don’t want to turn off this warning everywhere, but for this specific function I would like to turn it off. Is there a way?