Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Const correctness warnings c++

Does anyone know of any warnings that C++ compilers provide that help to enforce const correctness? For instance, it would be nice to have a warning produced by any C++ method that contains a non-const parameter that is never modified inside of the method. I see that there is a gnu compiler warning called -Wsuggest-attribute=const; however, when I use this flag I get an error saying that it is not recognized. Any ideas why?

like image 398
user809409 Avatar asked Apr 14 '12 19:04

user809409


People also ask

Does C have const correctness?

In C, C++, and D, all data types, including those defined by the user, can be declared const , and const-correctness dictates that all variables or objects should be declared as such unless they need to be modified.

Is Const correctness important?

The benefit of const correctness is that it prevents you from inadvertently modifying something you didn't expect would be modified.

What does const mean in C?

The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it.

Does const improve performance C?

const correctness can't improve performance because const_cast and mutable are in the language, and allow code to conformingly break the rules. This gets even worse in C++11, where your const data may e.g. be a pointer to a std::atomic , meaning the compiler has to respect changes made by other threads.


3 Answers

I don't think such a warning exists, mostly because it would be useless. Just because a parameter is not modified inside the call, doesn't mean it should be made const just for the sake of it.

Think of virtual functions. Perhaps the designer of the base class, although not modifying the parameter in the base class, wants to leave it up to an extending class whether or not to modify that parameter.

Also, think of large applications, where modifying interfaces or API's or whatever costs a lot. You might not need to modify the parameter now, but intend to do so in the future. You're not going to make it const now, and force a full rebuild and probably risk errors in the future when you remove the const.

like image 72
Luchian Grigore Avatar answered Nov 07 '22 15:11

Luchian Grigore


-Wsuggest-attribute=const

This analysis requires option

-fipa-pure-const

which is enabled by default at

-O 

and higher

like image 44
AchmadJP Avatar answered Nov 07 '22 15:11

AchmadJP


Careful, a const parameter like this one:

void myFunc(int const param);

does not belong to the interface. It belongs to the local scope of the function implementation. In fact, this function:

int inc(int const param) { return param+1; }

may be declared as

int inc(int param);

It is not a violation of the const correctness paradigm to claim the right to modify a variable but not actually do it.

If you are worried about const_cast you can either not use it in the first place or simply grep for it in your code base.

like image 4
bitmask Avatar answered Nov 07 '22 16:11

bitmask