gpt4 book ai didi

c++ - 如何在不指定模板的情况下声明函数模板指针typedef?

转载 作者:行者123 更新时间:2023-12-02 09:55:25 24 4
gpt4 key购买 nike

#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> >。由于在示例中 resresf都可以在不指定第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>,因此 dtypedouble。由于 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);
}

那是否意味着 functor具有动态类型?这不适用于C++的(静态)类型系统,并且如果这合法,它可能会很快失去控制。您想要 std::vector<AlgoFunction>就是这种情况:您必须存储一个具体类型。否则,程序将需要根据运行时信息动态实例化一个函数:模板参数必须在编译时已知。

如果您提前知道类型,一种可能的选择是使用可能实例化的可能类型的 std::variant。也就是说,类似

std::vector<std::variant<AlgoFunction<float>, AlgoFunction<double>>>;

vector 的每个元素是否应提供一个或另一个,否则使用

std::vector<std::tuple<AlgoFunction<float>, AlgoFunction<double>>>;

vector 的每个元素是否适用于任何一种类型。

这是否有用,是否值得增加复杂性,取决于您。

关于c++ - 如何在不指定模板的情况下声明函数模板指针typedef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60556586/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com