【发布时间】:2012-01-17 21:29:35
【问题描述】:
(5.2.10/6) C++03 指向函数的指针可以显式转换为指向不同类型函数的指针。调用函数的效果 通过指向函数类型 (8.3.5) 的指针 函数定义中使用的类型未定义。除了 将“指向 T1 的指针”类型的右值转换为“指针”类型 到 T2"(其中 T1 和 T2 是函数类型)并返回其原始 type 产生原始指针值,这种指针的结果 转换未指定。 [注:另见 4.10 了解更多细节 指针转换。]
以下是我正在尝试做的事情,虽然很明显将fp1 转换为fp2 的结果将产生一个原始指针,但同时标准中的措辞变为"The result of such a pointer conversion is unspecified" 什么是这个意思吗?
int f() { return 42; }
int main()
{
void(*fp1)() = reinterpret_cast<void(*)()>(f);
int(*fp2)() = reinterpret_cast<int(*)()>(fp1);
// Safe to call the function ?
fp2();
}
【问题讨论】:
标签: c++ function-pointers reinterpret-cast