我可以扭转数组到指针的衰变过程? [英] Can I reverse the process of array-to-pointer decay?
本文介绍了我可以扭转数组到指针的衰变过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是合法的一个指针转换到一个数组的第一个元素的指针整个数组?
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?
}
这对我的编译器打印10,但我不知道,如果C ++标准允许它。
This prints 10 on my compiler, but I'm not sure if the C++ standard allows it.
推荐答案
没有,这是不合法的(因为它的不确定行为)。一个指针,指向整个数组是&放大器;一个
不是 P
。基本上,你投1指针到另一个。该标准描述了所有允许的转换,这个人是不是其中之一。
No, it's not legal(as in it's Undefined Behavior). A pointer to the whole array is &a
not p
. Basically, you're casting one pointer to another. The standard describes all the allowed conversions and this one is not among them.
这篇关于我可以扭转数组到指针的衰变过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文