为什么一旦换行typedef函数签名与原始签名不匹配

为什么此代码先打印1,然后打印0?

typedef void (* GLFWkeyfun)(GLFWwindow*,int,int);
std::cout << std::is_same<GLFWkeyfun,void(*)(GLFWwindow*,int)>::value << std::endl;
std::cout << std::is_same<std::function<GLFWkeyfun>,std::function<void(GLFWwindow*,int)>>::value << std::endl;
liushui405133466 回答:为什么一旦换行typedef函数签名与原始签名不匹配

请注意,GLFWkeyfun是函数 pointer 的类型。而是将函数类型指定为std::functionstd::function<void(GLFWwindow*,int,int)>的模板参数。

我们应该将函数类型指定为std::function。您可以在GLFWkeyfun上应用std::remove_pointer,例如

std::cout << std::is_same<std::function<std::remove_pointer_t<GLFWkeyfun>>,//                                      ^^^^^^^^^^^^^^^^^^^^^^          ^ 
                          std::function<void(GLFWwindow*,int)>>::value
          << std::endl;

如果您的编译器不支持C ++ 14,那么

std::cout << std::is_same<std::function<std::remove_pointer<GLFWkeyfun>::type>,//                                      ^^^^^^^^^^^^^^^^^^^^          ^^^^^^^
                          std::function<void(GLFWwindow*,int)>>::value
          << std::endl;

Demo

本文链接:https://www.f2er.com/3154581.html

大家都在问