-1

A lot of books and experts say when talking about declaring functions/methods that you should put const everywhere you can unless you know that you need to modify that parameter. But I haven't seen much const pointer to const in the codes. Though if I have a pointer, and function dosn't need to modify neither pointer itself, nor pointed value, it seems to be a good choice to write this parameter as a const pointer to const. But mostly I've seen in such cases pointer to const is used. Is there any downside of declaring parameter as const pointer to const comparing with just pointer to const?

Johy
  • 263
  • 2
  • 10
  • 3
    related/dupe: https://stackoverflow.com/questions/117293/use-of-const-for-function-parameters – NathanOliver Feb 03 '21 at 22:30
  • @NathanOliver I'm asking specifically about pointers – Johy Feb 03 '21 at 22:32
  • 5
    The same thing applies. If you are passing by value, `const` is basically pointless except to make sure you don't make a mistake in the function itself. – NathanOliver Feb 03 '21 at 22:33
  • While on one hand, it could prevent mistake, it will also require you to either make an extra copy or remove const if you want to change the value inside the function. Let say that if the pointer is null, you want to use some global default instead, then you either have to recompile all dependant code or make an extra copy in code (then having 2 pointers might cause bugs if the wrong one is used), so in the end, it does not give much benefit in general. – Phil1970 Feb 03 '21 at 23:15

3 Answers3

1

There is no downside and it has the upside that the compiler will warm you with an error if you accidentally attempt to overwrite the value of the pointer (just like with declaring non-pointer parameters as const).

I suspect the reason why people don't use const pointer to const more often is that it subjectively looks a little clunky but that's no reason not to use it in your own code. And another reason is that using const everywhere is not always as common as using it sparingly.

Personally I tend to use const everywhere when possible except in function signatures when it doesn't change the external interface (the caller of the function doesn't care if it's const int or just int) only for the sake of keeping the function signature concise, but it's not uncommon to also use const in function parameters in general.

Jordan Melo
  • 1,193
  • 7
  • 26
1

Top level const in function signatures is nearly meaningless. You can add/remove it at will. (A reference to const is not top level)

Elsewhere, yes a const pointer to const data is reasonable.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
1

Is there any downside of declaring parameter as const pointer to const comparing with just pointer to const?

Same downsides apply as passing any parameter by const value. Perhaps some downsides apply to pointers slightly stronger:

  • Obviously, it prevents the parameter from being modified. Typically, pointers are used as iterators, which are generally used for iteration which requires modification of the iterator. If you don't need the pointer for iteration, then are you sure you need a pointer in the first place? ... You might, but those cases are quite rare.
  • It is unnecessarily longer and more complex to write and to read. This is especially noticeable in case of compound types such as pointers which inherently impose additional cognitive load. The constness is irrelevant information to the caller. Showing only relevant information, and keeping meaningless boilerplate minimal is essential to keep the source readable.
  • You cannot overload "top-level" const parameter with a non-const parameter and vice versa. Both declare the same function! Restraining oneself to only use non-const parameters reduces the risk of being confused by the restriction on overloads.
  • It isn't conventional. As you wrote: But mostly I've seen .... Conventionality is a very weak argument, but unless you can provide a better argument against it, it is still an argument.

Sure, these aren't the strongest of downsides and somewhat subjective... but then again, so are the upsides. So in the end, it doesn't matter much one way or the other. Perhaps a middle ground is to declare the parameter non-const in header but decare it const in the definition.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Perhaps a middle ground is to declare the parameter non-const in header but decare it const in the definition. -In this case I would expect linker error when trying to call this function, as signatures are different.. – Johy Feb 06 '21 at 21:33
  • @Johy Regardless of what you expect, there would be no problem. A function with const paramer and a function with non-const parameter are treated as the same function. – eerorika Feb 06 '21 at 21:57