Is it legal as per the C++ standard to convert a pointer or reference to a fixed array (e.g. T(*)[N]
or T(&)[N]
) to a pointer or reference to a smaller fixed array of the same type and CV qualification (e.g. T(*)[M]
or T(&)[M]
)?
Basically, would this always be well-formed for all instantiations of T
(regardless of layout-type):
void consume(T(&array)[2]);
void receive(T(&array)[6])
{
consume(reinterpret_cast<T(&)[2]>(array));
}
I don't see any references to this being a valid conversion in:
However, it appears that all major compilers accept this and generate proper code even when optimized when using T = std::string
(compiler explorer)(not that this proves much, if it is undefined behavior).
It's my understanding that this should be illegal as per the type-system, since an object of T[2]
was never truly created, which means a reference of T(&)[2]
would be invalid.
I'm tagging this question c++11 because this is the version I am most interested in the answer for, but I would be curious to know whether this answer is different in newer versions a well.