[英]How do I add a template function pointer as a value parameter to a map
我有一个工作框架,在该框架下,我使用地图来运行给定特定枚举的特定功能。 刚才这导致我拥有很多功能,所有功能都具有相同的签名,并且很多功能都具有相同的代码。 因此,我想提供一个模板函数指针作为映射中的值。 这是工作设置:
QMap <ENUM,OutputWidget *(MainWindow::*)()>func_map;
OutputWidget *handle_first();
OutputWidget *handle_second();
func_map{{FIRST,&MainWindow::handle_first},{SECOND,&MainWindow::handle_second}};
OutputWidget *output1 = (this->*func_map[FIRST])();
OutputWidget *output2 = (this->*func_map[SECOND])();
我也希望能够更改func_map来处理此签名,但是我还无法弄清楚如何:
template <typename INPUTTYPE,typename OUTPUTTYPE> OUTPUTTYPE *handle_all();
我已经尝试过将此模板添加到地图中;
func_map{{FIRST,&MainWindow::handle_first},SECOND,&MainWindow::handle_second},{THIRD,&MainWindow::handle_all<FirstInputWidget,ThirdOutputWidget>}}
但这导致了以下错误:
../TemplateTest/mainwindow.cpp: In constructor ‘MainWindow::MainWindow(QWidget*)’:../TemplateTest/mainwindow.cpp:9:14: error: no matching function for call to ‘QMap<ENUM, OutputWidget* (MainWindow::*)()>::QMap(<brace-enclosed initializer list>)’
}
能够添加此模板将减少很多代码。 如果有人对如何实现这一点有任何想法,我将不胜感激。
谢谢
这里没有实际问题,您只需要获取函数模板实例的地址即可:
func_map{
{FIRST, &MainWindow::handle_all<FirstType, OutputWidget>},
{SECOND, &MainWindow::handle_all<SecondType, OutputWidget>}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.