   [C++] Const: Is 'const char *const' reasonable?
  Hey, while making my code const-correct, I encountered this and didn't know what to choose. I used to pass a 'char *', which is simply a string (not a buffer) to a function. But since it's only read or copied, I can make it const.Is there any reason to use 'const char *const' for this? Or is 'const char *' a better choice? The same goes to other pointers which are only read or copied. Should I just make the pointer const or also the data it's pointing to? Or: should I make anything const when it's possible, or are there situations that when it's possible it shouldn't be const?Thanks   
  When making a function that takes an int, do you define the function as taking an int, or a const int? Why?   ;  I usually prefer to declare everything that can be declared const as const, except for pass-by-value parameters because I don't want to expose that information in the function declaration.So I'd declare a local char* that won't change as a const char* const, but a char* parameter that won't change as a const char*.   ;Quote:Original post by NitageI usually prefer to declare everything that can be declared const as const, except for pass-by-value parameters because I don't want to expose that information in the function declaration.A pointer IS a pass-by-value parameter.   ;  I think that's what he was saying. He wants to pass the char by const pointer, but the pointer itself is not marked const since it's passed by value.   ;Quote:Original post by SneftelQuote:Original post by NitageI usually prefer to declare everything that can be declared const as const, except for pass-by-value parameters because I don't want to expose that information in the function declaration.A pointer IS a pass-by-value parameter.Hence it's passed as a non-constant address since that might be considered an implementation detail, just like he said.It sounds like a reasonable principle in general. Though personally I would also make an exception for class fields.edit: Too late... Again.   ;Quote:Original post by SneftelQuote:Original post by NitageI usually prefer to declare everything that can be declared const as const, except for pass-by-value parameters because I don't want to expose that information in the function declaration.A pointer IS a pass-by-value parameter.Surely you can also pass pointers by reference:void fun (int*&p) {   p = reinterpret_cast<int*> (0xDEAD);}#include <iostream>int main () {   int* i = reinterpret_cast<int*>(0xBEEF);   std::cout << i << '\n';   fun (i);   std::cout << i << '\n';}P.S.: I had a bigger, very philosophical post about that, when leaving common parlance, you somehow always pass by value. But I am a tad underdosed on caffeine and could not verify myself.   ;  Sidenote: I like the interpretation of the pointer syntax where one reads right to left:const int * const p; -> p is a const pointer to an integer which is const But above can be confusing. There's an alternate syntax:int const * const p; -> p is a const pointer to a const integerint const * p; -> p is a pointer to a const integerint * const p; -> p is a const pointer to an integerint * p; -> p is a pointer to an integerint const ** const p; -> p is a const pointer to a pointer to const integerint const * const * const p; -> p is a const pointer to a const pointer to const integerint const * const * const & p; -> p is a reference to a const pointer to a const pointer to const integerint const * const & p; -> p is a reference to a const pointer to const integer   ;Quote:Original post by SneftelWhen making a function that takes an int, do you define the function as taking an int, or a const int? Why?Hmm, well if I'd have a function that takes an int (not a pointer, not a reference), the passed int would be copied and thus constness doesn't matter. So I would leave it int, not const int.Ah wait, now I see...when passing a pointer, the pointer is copied of course, so it doesn't matter if its const or not. Only the memory its pointing to matters.Thanks ;)   ;  I see const correctness as something that should add value to the code with the optional bonus of some optimizations the compiler might be able to make. Doing const correctness for the sake of having as much const as possible is a wrong cause.This is true for most language features.If you write const type* then this means the pointer points at something that cannot be changed (via this pointer). This is something that adds value to the code, as it expresses an idea and prevents accidential modification and may allow the compiler to make optimisations based on that "contract" (declaring something const can be seen as a contract between you and the compiler).Now, on the other hand const type* const means the same as the above, plus you cannot change the pointer to point to something different at a later time. Do I care, does the compiler care? Does it make anything clearer? Does it allow for any extra optimisations?Basically, this reimplements the functionality of a reference, but with a much more hideous syntax. As such, it probably doesn't add much value to the code.   