Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it better to remove "const" in front of "primitive" types used as function parameters in the header?

In the code review process, one of my coworkers mentioned to me that "const"s in front of "primitive types" used as a function parameter in a header is meaningless, and he recommended to remove these "const"s. He suggested using "const" only in the source file in such cases. Primitive types mean types such as "int", "char", "float", etc.

The following is example.

example.h

int ProcessScore(const int score); 

example.cc

int ProcessScore(const int score) {   // Do some calculation using score   return some_value; } 

His suggestion is doing as follows:

example.h

int ProcessScore(int score);  // const is removed here. 

example.cc

int ProcessScore(const int score) {   // Do some calculation using score   return some_value; } 

But I'm somewhat confused. Usually, the user will look at only the header, so if there is inconsistency between the header and the source file, it might cause confusion.

Could anyone give some advice on this?

like image 213
chanwcom Avatar asked Sep 19 '17 05:09

chanwcom


People also ask

Should function parameters be const?

Always use const on function parameters passed by reference or pointer when their contents (what they point to) are intended NOT to be changed. This way, it becomes obvious when a variable passed by reference or pointer IS expected to be changed, because it will lack const .

Why would we put the const keyword in front of a pass by reference parameter?

The const keyword in front of the object name is used to guarantee that your function does not modify the objects that are passed to the function by reference or pointer. Not only will this tell other programmers that your function is safe to use, it is also strictly enforced by the compiler.

Is const useless?

Adding const to a value-type parameter in the function declaration is useless. The caller won't care whether the parameter is modified or not because the caller's data won't be affected in any way.

Why are const reference parameters useful in C ++?

Passing by const reference offers the same primary benefit as pass by reference (avoiding making a copy of the argument), while also guaranteeing that the function can not change the value being referenced.


1 Answers

For all types (not just primitives), the top level const qualifiers in the function declaration are ignored. So the following four all declare the same function:

void foo(int const i, int const j); void foo(int i, int const j); void foo(int const i, int j); void foo(int i, int j); 

The const qualifier isn't ignored inside the function body, however. There it can have impact on const correctness. But that is an implementation detail of the function. So the general consensus is this:

  1. Leave the const out of the declaration. It's just clutter, and doesn't affect how clients will call the function.

  2. Leave the const in the definition if you wish for the compiler to catch any accidental modification of the parameter.

like image 173
StoryTeller - Unslander Monica Avatar answered Sep 17 '22 17:09

StoryTeller - Unslander Monica