1

Does the following usage of const have any value?

void Func(const bool state) {
    std::cout << "The state is: " << state << std::endl;
}

const references are certainly recommended and I surely see the benefit of it. But, coming to simple const addition to non-reference parameter. Is that of any use?

AdeleGoldberg
  • 1,289
  • 3
  • 12
  • 28
  • 1
    If you put `state = false;` in the body (intentionally or mistakenly), the compiler will error. That can be valuable if it was indeed a mistake. – Eljay Apr 01 '20 at 19:56
  • Yes. I know that but my question was more towards - "Is it a good programming practice". But looking that the other associated discussion, it sounds like not. I got my answer. Thanks a lot. – AdeleGoldberg Apr 01 '20 at 20:07

1 Answers1

2

These function declarations

void Func(const bool state);

and

void Func(bool state);

are equivalent and declare the same one function.

You even may include the both declarations in a compilatfion units.

However if the parameter is declared with the qualifier const then inside the function definition it may not be changed.

Pay attention to that parameters of a function are its local variables.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335