指针到功能和指针到对象C和C之间投下++ [英] Casts between pointer-to-function and pointer-to-object in C and C++
问题描述
我错了有关以下内容?
C ++标准说,指针到函数和指针到对象的(反面)之间的转换是conditionnaly支持与实现定义的语义,而所有的C标准说,这是在任何情况下非法的,对吧?
C++ standards says that conversion between pointer-to-function and pointer-to-object (and back) is conditionnaly-supported with implementation-defined semantics, while all C standards says that this is illegal in all cases, right?
void foo() {}
int main(void)
{
void (*fp)() = foo;
void* ptr = (void*)fp;
return 0;
}
ISO / IEC 14882:2011
5.2.10 Reinter preT施放[expr.reinter pret.cast]
5.2.10 Reinterpret cast [expr.reinterpret.cast]
8转换函数指针,指向一个对象的指针型或副
有条件支持反之亦然。这种转换的意思是
实现定义的,但如果实现支持
转换在两个方向上,一种类型的一个prvalue转换为
其他类型和背面,可能具有不同cvqualification,
应产生原始指针值。
8 Converting a function pointer to an object pointer type or vice versa is conditionally-supported. The meaning of such a conversion is implementation-defined, except that if an implementation supports conversions in both directions, converting a prvalue of one type to the other type and back, possibly with different cvqualification, shall yield the original pointer value.
我现在不能找到它在C标准什么...
I can't find anything about it in C standard right now...
推荐答案
你是对的,C(99)标准只字未提从指针到函数指针到对象的转换,所以这是不确定的行为。 *
You're right, the C(99) standard says nothing about conversion from pointer-to-function to pointer-to-object, therefore it's undefined behaviour.*
<子> *注意,但是,它确实定义指针到函数类型之间的行为。
这篇关于指针到功能和指针到对象C和C之间投下++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!