This mostly addresses the second line: best practices, assignments, function parameters etc.
General practice. Try to make everything const
that you can. Or to put that another way, make everything const
to begin with, and then remove exactly the minimum set of const
s necessary to allow the program to function. This will be a big help in attaining const-correctness, and will help ensure that subtle bugs don't get introduced when people try and assign into things they're not supposed to modify.
Avoid const_cast<> like the plague. There are one or two legitimate use cases for it, but they are very few and far between. If you're trying to change a const
object, you'll do a lot better to find whoever declared it const
in the first pace and talk the matter over with them to reach a consensus as to what should happen.
Which leads very neatly into assignments. You can assign into something only if it is non-const. If you want to assign into something that is const, see above. Remember that in the declarations int const *foo;
and int * const bar;
different things are const
- other answers here have covered that issue admirably, so I won't go into it.
Function parameters:
Pass by value: e.g. void func(int param)
you don't care one way or the other at the calling site. The argument can be made that there are use cases for declaring the function as void func(int const param)
but that has no effect on the caller, only on the function itself, in that whatever value is passed cannot be changed by the function during the call.
Pass by reference: e.g. void func(int ¶m)
Now it does make a difference. As just declared func
is allowed to change param
, and any calling site should be ready to deal with the consequences. Changing the declaration to void func(int const ¶m)
changes the contract, and guarantees that func
can now not change param
, meaning what is passed in is what will come back out. As other have noted this is very useful for cheaply passing a large object that you don't want to change. Passing a reference is a lot cheaper than passing a large object by value.
Pass by pointer: e.g. void func(int *param)
and void func(int const *param)
These two are pretty much synonymous with their reference counterparts, with the caveat that the called function now needs to check for nullptr
unless some other contractual guarantee assures func
that it will never receive a nullptr
in param
.
Opinion piece on that topic. Proving correctness in a case like this is hellishly difficult, it's just too damn easy to make a mistake. So don't take chances, and always check pointer parameters for nullptr
. You will save yourself pain and suffering and hard to find bugs in the long term. And as for the cost of the check, it's dirt cheap, and in cases where the static analysis built into the compiler can manage it, the optimizer will elide it anyway. Turn on Link Time Code Generation for MSVC, or WOPR (I think) for GCC, and you'll get it program wide, i.e. even in function calls that cross a source code module boundary.
At the end of the day all of the above makes a very solid case to always prefer references to pointers. They're just safer all round.
int *(*)(char const * const)
. Start to the right of the parenthesized*
then we have to move left:pointer
. Outside the parens, we can move right:pointer to function of ...
. Then we have to move left:pointer to function of ... that returns pointer to int
. Repeat to expand the parameter (the...
):pointer to function of (constant pointer to constant char) that returns pointer to int
. What would the equivalent one-line declaration be in a easy-reading language like Pascal? – Mark K Cowan Jul 9 '15 at 17:08function(x:^char):^int
. There function types are imply a pointer to a function so no need to specify it, and Pascal doesn't enforce const correctness. It can be read from left to right. – Calmarius Jul 9 '15 at 20:54