#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
enum Op{ADD, SUB, MUL, DIV, MATMUL};
template <typename dtype>
using AlgoFunction = double(*)(const vector<dtype> &, Op);
// for example, the sum function doesn't require template.
// just write sum(a), not sum<float>(a)
template <typename dtype>
double sum(vector<dtype> inputs) {
dtype summer = inputs[0];
for (int i=1; i<inputs.size(); i++) summer = summer + inputs[i];
return double(summer);
}
// i need to do ask this question because I perform the same
// algorithm (linearAlgo, ...) on different types of data
// (dtype = float, double, matrix<float>, matrix<double>, ...
template <typename dtype>
inline dtype numOperate(const dtype &a, const dtype &b, Op op) {
if (op==ADD) return a + b;
if (op==SUB) return a - b;
if (op==MUL) return a * b;
if (op==DIV) return a / b;
}
template <typename dtype>
double linearAlgo(const vector<dtype> &inputs, Op op) {
dtype summer = inputs[0];
for (int i=1; i<inputs.size(); i++) summer = numOperate(summer, inputs[i], op);
return double(summer);
}
template <typename dtype>
double reverseLinearAlgo(const vector<dtype> &inputs, Op op) {
int n = inputs.size();
dtype summer = inputs[n-1];
for (int i=n-2; i>=0; i--) summer = numOperate(summer, inputs[i], op);
return double(summer);
}
template<typename dtype>
vector<double> run(vector<dtype> inputs, Op op, double (*func)(const vector<dtype>&, Op)) {
vector<double> res;
res.push_back(func(inputs, op));
return res;
}
int main()
{
vector<float> a;
vector<double> b;
a.push_back(1); a.push_back(2); a.push_back(3);
b.push_back(1); b.push_back(2); b.push_back(3);
vector<double> res = run(a, ADD, linearAlgo); // allowed without specifying template
vector<double> resf = run(b, ADD, linearAlgo); // still work with multiple data type
// I want to do this assignment without specifying the template.
// in the above linear, linearAlgo (no specifying template) is possible, why not here ?
AlgoFunction<float> functor = reverseLinearAlgo; // works, but I don't want it
//AlgoFunction functor = reverseLinearAlgo; // I want to do this. compile error
vector<double> res2 = run(a, ADD, functor);
cout << res[0] << "\n";
cout << res2[0];
return 0;
}
所以我有一个功能模板指针
template <typename dtype>
using AlgoFunction = double(*)(const vector<dtype> &, Op);
指向这样的功能
template <typename dtype>
double linearAlgo(const vector<dtype> &inputs, Op op) {
dtype summer = inputs[0];
for (int i=1; i<inputs.size(); i++) summer = numOperate(summer, inputs[i], op);
return double(summer);
}
我知道可以使用模板函数指针而不指定模板。例如:
vector<float> a;
a.push_back(1); a.push_back(2); a.push_back(3);
vector<double> res = run(a, ADD, linearAlgo); // allowed without specifying template
但是如果我声明类型为AlgoFunction
的变量,则编译器会强制我指定模板。
//AlgoFunction<float> functor = reverseLinearAlgo; // works, but I don't want it
AlgoFunction functor = reverseLinearAlgo; // I want to do this. compile error
这不好,因为我有很多类型的数据dtype
,并且我不想为每个数据再次指定模板。
那么如何声明AlgoFunction functor;
而不是AlgoFunction<some_datatype_name> functor;
?
谢谢。
编辑:目标是使用vector<AlgoFunction> functors
而不是vector<AlgoFunction<data_type> >
。由于在示例中,res
和resf
都可以在不指定第3个参数的模板的情况下进行计算,因此我想知道vector<AlgoFunction>
是否可行。
您不能。我怀疑造成这种混淆的原因是缺少“功能”和“功能模板”之间的区别。
为了解释您的第一个示例为何起作用,首先让我们检查执行run(a, ADD, linearAlgo);
时实际发生的情况。提醒一下,我们有:
template <typename dtype>
using AlgoFunction = double(*)(const std::vector<dtype>&, Op);
template <typename dtype>
std::vector<double> run(const std::vector<dtype>&, Op,
double(*)(const std::vector<dtype>&, Op));
等效地,我们本来可以拥有以下内容:
std::vector<double> run(const std::vector<dtype>&, Op, AlgoFunction<dtype>);
因为AlgoFunction
只是一个别名。
现在,当我们这样做时:
std::vector<double> a;
run(a, ADD, linearAlgo);
我们知道run
的第一个参数是std::vector<dtype>
,因此std::vector<double>
是dtype
。由于double
只是模板,即“模式”,因此我们无法从第三个参数确定有关dtype
的任何信息。
由于我们知道linearAlgo
必须为dtype
,因此我们可以选择并实例化double
–即linearAlgo<dtype>
–作为我们的函数,因为它符合我们的签名,并且一切正常。
现在,这和这有什么关系?
linearAlgo<double>
在这种情况下,我们正在尝试创建一个变量。
AlgoFunction functor = reverseLinearAlgo;
只是一个函数模板,而不是实际的函数,并且我们没有任何其他上下文来确定reverseLinearAlgo
的实际类型。因此,编译器错误。
而且,这实际上意味着什么? functor
的类型取决于您在哪里使用?如果我做了functor
,auto x = functor;
会是哪种类型?如果我做了类似的事情
x
这是否意味着
AlgoFunction functor = reverseLinearAlgo; if (test) { std::vector<float> x; functor(x, ADD); } else { std::vector<double> x; functor(x, ADD); }
具有动态类型?这不适用于C ++的(静态)类型系统,并且如果这合法,它可能会很快失去控制。您希望functor
的情况就是这样:您必须存储一个具体类型。否则,程序将需要根据运行时信息动态实例化一个函数:必须在编译时知道模板参数。
如果您提前知道类型,一种可能的选择是使用可能实例化的std::vector<AlgoFunction>
类型。也就是说,类似
std::variant
如果向量的每个元素都应该提供一个或另一个,或者使用
std::vector<std::variant<AlgoFunction<float>, AlgoFunction<double>>>;
如果向量的每个元素都可用于任何一种类型。
这是否有用,是否值得增加复杂性,取决于您。
可以做您想做的事,但是用C ++来实现是很麻烦的,因为如果您想认真地实现这样的事情,就必须进行手工类型检查。