Here’s a code snippet that hopefully conveys what I’m trying to do:
void updatePointer(const int*& i)
{
i++;
}
int main() {
int array[5];
int* arrayPtr = array;
updatePointer(arrayPtr );
return 0;
}
This gives compiler error:
prog.cpp: In function ‘int main()’:
prog.cpp:16: error: invalid initialization of reference of type ‘const int*&’ from
expression of type ‘int*’
prog.cpp:5: error: in passing argument 1 of ‘void updatePointer(const int*&)’
Supposing that you could do it, you could write the following:
The purpose of
constis to ensure that user code cannot attempt to modify a const object unless it contains a const-cast (or equivalent). So the compiler has to refuse this code. Forbidding aconst int*&from binding to anint*is the only place in the code above that’s reasonable for the compiler to refuse: every other line is fine.It’s the same reason you can’t implicitly convert
int**toconst int **.Aside from the motivation in terms of const-safety, you can think if it in terms of
int*being a different type fromconst int*, that just so happens to be convertible to it. Likewise, you can convertinttodouble, but adouble&can’t bind to anintlvalue. That’s not the full reason, because actuallyint*andconst int*have the same size and representation, whereasintanddoubledon’t. So there could be a special-case to allow it if not for the fact that it would break the const system.The reason that C++ has both const and non-const overloads for
strchris related to this issue: your functionupdatePointermodifies its input rather than returning the updated value, but the principle is similar. The C-style singlestrchrallows you to “launder” a pointer-to-const into a pointer-to-non-const without a cast, and it’s a hole in the const system. C++ (a) has overloading and (b) has a stricter type system than C, so it closes that hole.If you want your real function
updatePointerto work likestrchr— examine the data pointed to and compute a new value for the pointer, then you’re in the same situation thatstrchris. That’s regardless of what it does with the new value (return it in the case ofstrchr, write it back in the case ofupdatePointer), because the issue is that you want the new pointer to have the same const-qualification as the input. You need to provide either const- and non-const overloads or a function template.If you only need your real function
updatePointerto move a pointer by a certain distance, regardless of the data pointed to, you could usestd::advanceinstead.