c++ - 将 "pointer to data"转换为 "pointer to function"

标签 c++ c

据我所知,在 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/

相关文章:

c++ - 从 Makefile (Qt) 中删除标志 (-fpermissive)

c++ - 启动后如何结束libsoundio流?

c++ - 带递归的可变参数函数

c++ - 循环条件的计算

c - 如何为 sem_timedwait 正确设置 timespec 以防止 EINVAL 错误

c - 在路径中找不到程序 g++

c++ - 头文件中的 "class example;"是什么意思?

c++ - 在 C/C++ 中多次包含头文件

c++ - 递归返回类型

c - 如何将 TargetLink 模型转换为 Simulink