在C和C ++中的指针到函数和指针到对象之间转换 [英] Casts between pointer-to-function and pointer-to-object in C and C++
问题描述
我错了以下?
C ++标准说,指针到函数和指针到对象(和背面)之间的转换是条件支持与实现定义的语义,而所有C标准说,这是非法的在所有情况下,是吗?
void foo }
int main(void)
{
void(* fp)()= foo;
void * ptr =(void *)fp;
return 0;
}
ISO / IEC 14882:2011 >
5.2.10 Reinterpret cast [expr.reinterpret.cast]
8转换函数指向对象指针类型或者
反面的指针是有条件支持的。这种转换的意义是
实现定义的,除了如果一个实现支持两个方向的
转换,将一个类型的prvalue转换为
另一个类型并返回,可能具有不同的cvqualification,
会产生原始的指针值。
现在我在C标准中找不到任何东西。 ..
没错,C(99)标准没有说明从指针到函数到指针的转换对象,因此它是未定义的行为。 *
*但是,定义指针到函数类型之间的行为。
Am i wrong about the following?
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 Reinterpret cast [expr.reinterpret.cast]
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.
I can't find anything about it in C standard right now...
You're right, the C(99) standard says nothing about conversion from pointer-to-function to pointer-to-object, therefore it's undefined behaviour.*
*Note, however, that it does define behaviour between pointer-to-function types.
这篇关于在C和C ++中的指针到函数和指针到对象之间转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!