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

后端 未结 5 926
说谎
说谎 2020-12-13 01:40

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

5条回答
  •  温柔的废话
    2020-12-13 02:28

    I thought that const is a hint for the compiler that some expressions don't change and to optimize accordingly. For example I was testing if a number is prime by looking for divisors up to the square root of the number and I thought that declaring the argument const would take the sqrt(n) outside of the for loop, but it didn't.

    It may not be necessary in the header, but then again, you could say that all you need is to not modify the argument and it is never necessary. I'd rather see const where it is const, not just in the source, but in the header too. Inconsistencies between declaration and definition make me circumspect. Just my opinion.

提交回复
热议问题