I am not super proficient in C++, but I was writing some code for a project of mine, and I had a situation effectively similar to this:
class Test {
public:
std::bitset<123> *abc;
void step_all() {
(*abc)[42] = false;
}
};
int main() {
Test().step_all();
}
In this situation, Clang-Tidy requests that I annotate step_all as const and given my lack of experience with the language, I found this particularly interesting.
Usually, static analyzers are a good way to learn a language and avoid bad practices; however, I don’t think the suggestion is appropriate in this case because the function/method actually does modify the state.
I was going to look into how to suppress the warning, but perhaps there is a way to not have the warning there in the first place…
The compiler doesn’t seem to complain about adding const, and I understand that to be because the actual pointer value isn’t being modified, even if the values at the heap address it points to is.
In this situation I think it’s misleading to annotate the function as const.
My question:
Is it the case that
- There is a better way to do this using some pointer wrapper or something in modern c++ (Change my implementation)
- Clang-Tidy could do better (Supress the warning)
- Clang-Tidy is actually correct (Annotate as const)
- Something else?