函数指针数组的模板 [英] Template of array of function pointers

查看:136
本文介绍了函数指针数组的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了一些功能,其中一些超载和一些模板,例如:

I've got a few functions, some of which are overloaded and some are templates, ex.:

void fun1 (vector<string> &a, int b);
void fun1 (vector<double> &a, int b);

template <typename t>
void fun2 (t a[], int b);

和等。我使用每个功能都有2数据类型的一个版本,一个是数组,另外一个是向量(字符串矢量,双打的向量,字符串数组和双打的数组)。现在的问题是,我可以创建一个指针数组模板?有没有这样做,除了任何方式:

and so on. Every function I use has a version for 2 data types, one is an array and the other one is a vector (a vector of strings, a vector of doubles, an array of strings and an array of doubles). The question is, can I create a template for an array of pointers? Is there any way of doing it, apart from:

void (*strvecfun[])(vector <string>&, long, long)={fun1, fun2};
void (*dblvecfun[])(vector <double>&, long, long)={fun1, fun2};
void (*strarrfun[])(string [], long, long)={fun1, fun2};

等等?

推荐答案

您可以使用

template<typename Func>
struct func_array {
  static Func *const data[];
};

template<typename Func>
Func *const func_array<Func>::data[] = { fun1, fun2 };

后来电话

func_array<void(std::vector<double>&, long, long)>::data[0](dvec, l1, l2);
func_array<void(std::vector<string>&, long, long)>::data[1](svec, l1, l2);
// ...and so forth

这需要有所有你要使用你把列表中的所有功能签名匹配超载,自然。

This requires that there are matching overloads for all the signatures you're going to use of all functions you put into the list, naturally.

这篇关于函数指针数组的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆