我正在使用 FLTK 来做我的 GUI 相关的事情,它需要将类型的函数void (*fn)( Fl_Widget*, void* )注册为小部件回调。我厌倦了手动创建函数转发器,将void*s解包为参数并从我的类中调用适当的静态函数来完成用户请求的工作。
我想出了一个解决方案,但它需要一个专用于函数类型和函数地址的类,这就是我遇到问题的地方。这是一些代码:
template< typename T, typename fn_ptr_t, fn_ptr_t fn_ptr >
struct Fl_Callback_package;
template< typename T, typename Return, typename... Params >
struct Fl_Callback_package< T, Return (*)( Params... ), /* What goes here? */ >
{
//...
};
Run Code Online (Sandbox Code Playgroud)
编辑:澄清一下 - 我不想要一个特定的函数来代替/* What goes here? */,而是我希望这个参数是Return (*fn_ptr)( Params... ). 当我尝试
template< typename T, typename Return, typename... Params >
struct Fl_Callback_package< T, Return (*)( Params... ), Return (*fn_ptr)( …Run Code Online (Sandbox Code Playgroud) c++ templates function-pointers template-meta-programming c++11
说我有函数指针R(*fp)(...A)。有什么方法可以获取类型R和类型列表A...吗?std::invoke_result_t似乎不起作用。而且我找不到参数列表的任何内容。
#include <iostream>
#include <cstdlib>
#include <type_traits>
typedef int(*fp_t)(int, float);
std::invoke_result_t<fp_t> x = 10;
int main()
{
std::cout << "Hello, Wandbox!" << std::endl;
std::cout << x << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
用c ++ 17编译会出现错误:
/opt/wandbox/gcc-head/include/c++/10.0.0/type_traits: In substitution of 'template<class _Fn, class ... _Args> using invoke_result_t = typename std::invoke_result::type [with _Fn = int (*)(int, float); _Args = {}]':
prog.cc:6:26: required from here
/opt/wandbox/gcc-head/include/c++/10.0.0/type_traits:2917:11: error: no type named 'type' in 'struct std::invoke_result<int (*)(int, float)>'
2917 | …Run Code Online (Sandbox Code Playgroud) c++ function-pointers variadic-templates template-argument-deduction c++17