如何在不指定模板的情况下使用声明函数模板指针typedef?

How to use declare a function template pointer typedef without specifying template?

#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 都可以在不指定第三个参数的模板的情况下计算,我想知道 vector<AlgoFunction> 是否可能。

你不能。我怀疑混淆是由于忽略了 "function" 和 "function template".

之间的区别

为了解释您的第一个示例为何有效,首先让我们检查一下您执行 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);

我们知道 runstd::vector<dtype> 的第一个参数是 std::vector<double>,因此 dtypedouble。我们无法从第三个参数确定任何关于 dtype 的信息,因为 linearAlgo 只是一个模板,一个 "pattern".

因为我们知道 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>>>;

如果向量的每个元素都应提供其中之一,否则使用

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

如果向量的每个元素都可以用于任一类型。

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

可以做你想做的事,但用 C++ 实现起来很麻烦,因为如果你想认真地实现这样的东西,你必须进行手动类型检查。

这里有一个快速的方法来做你想做的事,但要注意你需要的不仅仅是这个来做一些对严肃工作有用的东西,而且使用这种代码很容易搬起石头砸自己的脚:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct AlgoFunction {
    virtual double operator()(void *) = 0;
};

template <class T>
struct AF_Sum : public AlgoFunction {
    virtual double operator()(void * inputVec) {
        T res = T();
        vector<T>* pInput = (vector<T>*)inputVec;
        for (int i = 0; i < pInput->size(); ++i) {
            res += (*pInput)[i];
        }

        return (double) res;
    }
};

template <class T>
struct AF_Mean : public AlgoFunction {
    virtual double operator()(void * inputVec) {
        T res = T();
        vector<T>* pInput = (vector<T>*)inputVec;
        for (int i = 0; i < pInput->size(); ++i) {
            res += (*pInput)[i];
        }

        return (double) res / (double)pInput->size();
    }
};

int main()
{
    std::vector<float> vF{0.2, 0.3, 0.8};
    std::vector<int> vI{2, 5, 7};

    std::vector<AlgoFunction*> algoFunctions;

    algoFunctions.push_back(new AF_Sum<float>);
    algoFunctions.push_back(new AF_Mean<int>);

    cout << (*algoFunctions[0])(&vF) << endl;
    cout << (*algoFunctions[1])(&vI) << endl;

    return 0;
}

请注意,我没有清理堆分配的内存(通过 new),也没有实现您的所有功能;只是潜在解决方案的一个简单粗暴的例子。