据我所知,在 C89、C99 中不允许从指向数据的指针到指向函数的指针。那么 C++ 呢?
最佳答案
简短的回答:不需要实现来支持这样的强制转换。
它可能允许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 cv-qualification, shall yield the original pointer value.
非嵌入式实现通常允许它用于动态链接(例如dlsym()
)。但严格来说,它并不需要工作。
关于c++ - 将 "pointer to data"转换为 "pointer to function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29096929/