2

Possible Duplicate:
Use of 'const' for function parameters

For example,

void Func(int para);

If I know I don't want to change para's value in Func(...), should I instead declare para as "const int" to guarantee that? i.e.

void Func(const int para);

BTW, I think

void Func(const int &para);

is not an appropriate alternative sometimes because const int& is usually implemented by underlying pointer, thus it is essentially equivalent to

void Func(const int *p_para);

The book "C++ Coding Standards: 101 Rules" p31 says that "void Func(const int para);" is bad as it will "confuse" the readers of the header files. I'm not sure though...

After thought it over, I think a good solution is to declare it as "void Func(int para);" and use "void Func(const int para) {...}" when implement it. I find that the word "const" in "void Func(const int para);" will be silently dropped by the compiler...

Community
  • 1
  • 1
updogliu
  • 6,066
  • 7
  • 37
  • 50

5 Answers5

5

If you do this at all (and some people do) then you should put the const in the definition (where it is useful) but not the declaration (where it is noise, it doesn't affect the caller).

So:

void F(int param);

...


void F(int const param) {
   ...
}
Alan Stokes
  • 18,815
  • 3
  • 45
  • 64
  • That's right. If you use const in the declaration but not in the definition, you can still modify the variable, i.e. the const in the declaration will be ignored. – updogliu Feb 10 '12 at 00:21
3

Declaring an argument as const serves three purposes:

  1. It tells the compiler that the value is constant, which may enable certain optimizations. Given today's "smart" compilers, I'm not sure that it's still relevant.

  2. It tells programmers at a glance that the value does not change within the function, which may be helpful as they debug/enhance the code.

  3. It makes the compiler actually enforce your assertion that the function does not change the value.

In general it's a good idea to declare variables as const if you can, for the same reason it's a good idea to eliminate compiler warnings: it makes your code easier to support.

Adam Liss
  • 47,594
  • 12
  • 108
  • 150
  • I don’t think this ever enabled any optimisations. Points 2 and 3 are good though. – Konrad Rudolph Feb 09 '12 at 15:18
  • Since the argument is an `int` (as opposed to an object that might have a complicated constructor), the compiler doesn't need to make a copy of the value for use within the function; it can directly use the contents of the caller's variable. – Adam Liss Feb 09 '12 at 15:25
  • @KonradRudolph I suspect `const` for a parameter can work as a blunt version of `restrict`. That is, if it matters to the compiler that several read-write parameters passed by address to a function may point to the same address, but it wouldn't matter if they were read-only. I'm not sure if such scenarios do exist though. – Lundin Feb 09 '12 at 15:26
  • There is a possible optimisation in that a copy does not need to be made. – Ed Heal Feb 09 '12 at 15:26
0

const int para: useful to you as it stops you modifying para by mistake. Not sure about the confusion issue.

const int &para: You should do this, indeed sometimes you have to if code elsewhere insists that you keep para constant (for example if you are writing a custom comparator function for a map). It's also more efficient than const int para - if you are not modifying para why do you need to copy it?

I don't think it matters what const int &para is implemented as underneath. What matters is that on top you can't modify it (without being evil) so the effect is the same as const int para but more efficient.

Sideshow Bob
  • 4,566
  • 5
  • 42
  • 79
  • 1
    No, no code will ever insist you do this. You are confusing this with const-ref arguments. – Konrad Rudolph Feb 09 '12 at 15:15
  • I'm pretty sure references are commonly implemented by pointers. You can find information at http://en.wikipedia.org/wiki/Reference_(C%2B%2B) – updogliu Feb 09 '12 at 15:26
  • For sure, they *are* pointers underneath but this is irrelevant because up above, the compiler won't let you change the thing they point to, and they can't be null or uninitialized. These things set them apart from pointers from a software engineering perspective: they are generally much safer to use. – Sideshow Bob Feb 10 '12 at 09:56
0

I think it will only serve to clarify things for readers of header files, by marking a parameter const it is effectively saying it is input only. Likewise the & reference operator is read as the parameter being modified in the function with effect on the passed parameter. This may be where the confusion arises, however const int &para is const "before" it is & reference and doesn't require much interpretation.

I confuse & and * notation because I am used to C where the & operator does not exist. But for C++, I would recommend you use it.

John
  • 6,433
  • 7
  • 47
  • 82
-1

You should not. in C, parameters are been passed by value, so the caller don't need to know what do you do with the int it pass. even if your function is void func(int), you can pass a const int to it.

void func(int a) {
 a=5;
}
...
const int b=7;
func(b);

In this code, b will still be 7, since the function only changed the local variable a.

Of course, if the function get a pointer, or a reference, it's different. if the declaration is void func(int &a), then the above code is illegal, since func changed b, which is const, and therefor if your function doesn't change the reference, and you want the possibility to pass a const int to it, you should declare the parameter as const.

asaelr
  • 5,438
  • 1
  • 16
  • 22