I have a function that receives float**
as an argument, and I tried to change it to take const float**
.
The compiler (g++
) didn't like it and issued :
invalid conversion from ‘float**’ to ‘const float**’
this makes no sense to me, I know (and verified) that I can pass char*
to a function that takes const char*
, so why not with const float**
?
This is a very tricky restriction. It is related to the aliasing rules of the language. Take a look at what the standards say, because I have faced this once before:
(Page 61)
[Note: if a program could assign a pointer of type T** to a pointer of type const T** (that is, if line //1 below was allowed), a program could inadvertently modify a const object (as it is done on line //2). For example,
int main() { const char c = 'c'; char* pc; const char** pcc = &pc; //1: not allowed *pcc = &c; *pc = 'C'; //2: modifies a const object }
—end note]