Reputation: 2193
So usually when you type arrays for function arguments you declare them like this:
f ( const float offset [ 3 ] )
But I've been working on a project that has them declared like so:
f ( const float ( &offset ) [ 3 ] )
What does that change even mean? To my knowledge we are already effectively passing around a pointer. What what coercing it to a reference like this even do?
Upvotes: 1
Views: 71
Reputation: 103693
f ( const float offset [ 3 ] )
In this case, the 3 is meaningless. offset
is not even an array, it is a pointer (const float*
). So this function will accept any float pointer, and an array of floats of any size will be accepted through decay.
float x2[2];
float x3[3];
float x4[4];
float* fp;
f(x2); // compiles
f(x3); // compiles
f(x4); // compiles
f(fp); // compiles
Furthermore, inside the function:
sizeof(offset) == sizeof(float*)
However,
f ( const float ( &offset ) [ 3 ] )
In this case, offset
is a reference to an array of 3 const floats. Only an array of 3 floats will be accepted as an argument.
f(x2); // does not compile
f(x3); // compiles
f(x4); // does not compile
f(fp); // does not compile
And inside the function:
sizeof(offset) == sizeof(float) * 3
Upvotes: 3