gpt4 book ai didi

c++ - 不同的函数指针是否相互兼容?

转载 作者:太空宇宙 更新时间:2023-11-04 14:49:19 25 4
gpt4 key购买 nike

今天我了解到函数指针和数据指针不一样,因此彼此不兼容(Why are function pointers and data pointers incompatible in C/C++?)。然而,我的问题是,不同的函数(非成员)指针是否相互兼容(以相同的方式实现)。

在代码中:

typedef void(*FuncPtr0)();
typedef void(*FuncPtr1)(int);

FuncPtr0 p0;
FuncPtr1 p1;

p0 = reinterpret_cast<FuncPtr0>(p1); // will this always work, if p1 really
p0(); // points to a function of type FuncPtr0

感谢您的帮助!

最佳答案

n3376 5.2.10/6

A function pointer can be explicitly converted to a function pointer of a different type. The effect of callinga function through a pointer to a function type (8.3.5) that is not the same as the type used in the definitionof the function is undefined. Except that converting a prvalue of type “pointer to T1” to the type “pointer toT2” (where T1 and T2 are function types) and back to its original type yields the original pointer value, the result of such a pointer conversion is unspecified.

关于c++ - 不同的函数指针是否相互兼容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20211398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com