I am confused that why following code is not able to compile
int foo(const float* &a) {
return 0;
}
int main() {
float* a;
foo(a);
return 0;
}
Compiler give error as:
error: invalid initialization of reference of type ‘const float*&’ from expression of type ‘float*’
When I try to pass without by reference in foo, it is compiling fine.
I think it should show same behavior whether I pass by reference or not.
Because it isn’t type-safe. Consider:
Any non-
constreference or pointer must necessarily be invariant in the pointed-to type, because a non-constpointer or reference supports reading (a covariant operation) and also writing (a contravariant operation).constmust be added from the greatest indirection level first. This would work: