Is it legal to cast a pointer to the first element of an array to a pointer to the entire array?
template<typename T, size_t N>
void whatever(T(&)[N])
{
std::cout << N << '\n';
}
int main()
{
int a[10];
int * p = a;
whatever(*(int(*)[10])(p)); // <-- legal?
}
This prints 10 on my compiler, but I’m not sure if the C++ standard allows it.
No, it’s not legal(as in it’s Undefined Behavior). A pointer to the whole array is
&anotp. Basically, you’re casting one pointer to another. The standard describes all the allowed conversions and this one is not among them.