是reinter pret_cast到C数组非法C ++ 11? [英] Is reinterpret_cast to C-style array illegal C++11?
问题描述
为什么以下code皱眉?
Why is the following code frowned upon?
double d[4] = {0,1,2,3};
reinterpret_cast<double[2]>(d);
海湾合作委员会声明它的类型双*'的无效强制转换为类型'双[2]
和铿锵声明 reinter $ P $从pt_cast双*'到'双[2]是不允许
现在的情况下,意图不是很明显,我想这code返回一个double [2]包含{0,1},pretty很像 reinter pret_cast&LT;双* GT;(四)
会。 (因此,我知道它会使用指针工作,所以这不是我问的)
Now in case the intent is not obvious, I would like this code to return a double[2] that contains {0,1}, pretty much like a reinterpret_cast<double*>(d)
would. (Hence I know it would work with pointers, so that's not what I'm asking)
推荐答案
这两种编译器是正确的。
Both compilers are correct.
reinter pret_cast
不是锤子,它是一个功能强大的precision工具。 reinter pret_cast
的所有用途都涉及到至少一个指针或引用类型作为源或目的,除了标识整体转换的退化情况(即 reinter pret_cast
从 INT
到 INT
是允许和不执行任何操作。)
reinterpret_cast
is not a hammer, it's a powerful precision tool. All uses of reinterpret_cast
have to involve at least one pointer or reference type as the source or as the destination, except for the degenerate case of an identity integral conversion (i.e. reinterpret_cast
from int
to int
is allowed and does nothing.)
这篇关于是reinter pret_cast到C数组非法C ++ 11?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!