c++ - 为什么一旦包装 typedef 函数签名与原始签名不匹配
问题描述
为什么这段代码打印 1 然后 0 ?
typedef void (* GLFWkeyfun)(GLFWwindow*,int,int,int,int);
std::cout << std::is_same<GLFWkeyfun, void(*)(GLFWwindow*, int, int, int, int)>::value << std::endl;
std::cout << std::is_same<std::function<GLFWkeyfun>, std::function<void(GLFWwindow*, int, int, int, int)>>::value << std::endl;
解决方案
请注意,这GLFWkeyfun
是一个函数指针类型。相反,函数类型被指定为 中std::function
的模板参数std::function<void(GLFWwindow*, int, int, int, int)>
。
我们应该将函数类型指定为std::function
. 您可以申请std::remove_pointer
,GLFWkeyfun
例如
std::cout << std::is_same<std::function<std::remove_pointer_t<GLFWkeyfun>>,
// ^^^^^^^^^^^^^^^^^^^^^^ ^
std::function<void(GLFWwindow*, int, int, int, int)>>::value
<< std::endl;
如果您的编译器不支持 C++14,那么
std::cout << std::is_same<std::function<std::remove_pointer<GLFWkeyfun>::type>,
// ^^^^^^^^^^^^^^^^^^^^ ^^^^^^^
std::function<void(GLFWwindow*, int, int, int, int)>>::value
<< std::endl;
推荐阅读
- python - ReactJs 应用程序和 Python 服务器中的 UTC 时差
- python - 在 vs code python 调试控制台中运行循环/多行
- python-3.x - 在循环中使用 urllib
- github - 我无法在 Visual Studio 代码终端中切换 github 帐户
- r - 如何让子图显示两个情节图?
- python - 如何规范化熊猫数据框中的多列字典
- javascript - ETHERPAD:禁用链接共享
- javascript - 如何在 javascript 中简化嵌套平面图
- r - left_join - 由于类型不兼容而无法加入
- mongodb - 如何将 MongoDB 与 node-red 连接