【问题标题】:How to use declare a function template pointer typedef without specifying template?如何在不指定模板的情况下使用声明函数模板指针 typedef?
【发布时间】:2020-03-06 02:06:54
【问题描述】:
#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&lt;some_datatype_name&gt; functor; 呢?

谢谢。

编辑:目标是使用vector&lt;AlgoFunction&gt; functors 而不是vector&lt;AlgoFunction&lt;data_type&gt; &gt;。由于示例中resresf都可以在不指定第三个参数的模板的情况下计算,所以我想知道vector&lt;AlgoFunction&gt;是否可能。

【问题讨论】:

  • “所以我有一个函数模板指针” - 或者你有别名吗? AlgoFunction 不是实例 - 它没有指向。
  • "编译器强制我指定模板" - 是的,您将AlgoFunction 设为template &lt;typename dtype&gt; double(*)(const vector&lt;dtype&gt; &amp;, Op); 的方便别名 - 编译器想知道特定的实例你喜欢。
  • "那么我如何声明 AlgoFunction functor; 而不是 &lt;AlgoFunction&lt;some_datatype_name&gt; functor;?" - 远远超出我的范围。据我所知,类/函数模板不会以实例的形式出现,除非被要求这样做。
  • @TedLyngmo 是的,我确实使用了别名。但是您可以在示例中看到,在将函数作为参数传递时我不需要指定模板以使其工作。所以我正在寻找一种在任何地方都可以做到这一点的方法。
  • 请注意,如果您不小心,生成的代码可能会慢得多。例如,指向函数的指针可能比内联函数慢。考虑像@​​987654340@这样的STL算法也可能很有用。

标签: c++ function templates function-pointers function-templates


【解决方案1】:

你不能。我怀疑这种混淆源于缺少“函数”和“函数模板”之间的区别。

为了解释为什么您的第一个示例有效,首先让我们检查一下当您执行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&lt;dtype&gt;std::vector&lt;double&gt;,因此dtypedouble。我们无法从第三个参数中确定关于 dtype 的任何信息,因为 linearAlgo 只是一个模板,一个“模式”。

既然我们知道dtype 必须是double,我们可以选择并实例化linearAlgo&lt;dtype&gt;——即linearAlgo&lt;double&gt;——作为我们的函数,因为它符合我们的签名,一切正常。


现在,这和这有什么关系?

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&lt;AlgoFunction&gt; 的情况:您必须存储一个具体类型。否则程序将需要根据运行时信息动态实例化一个函数:模板参数必须在编译时知道。


如果您提前知道类型,一个可能的替代方法是使用您可能实例化的可能类型的std::variant。也就是说,像

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

如果向量的每个元素都应该提供一个或另一个,否则使用

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

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

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

【讨论】:

  • 我以前从未使用过 C++17,所以 std::variant 对我来说是全新的。这实际上是我正在寻找的,因为我提前知道类型。谢谢
【解决方案2】:

可以做你想做的事,但是用 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),也没有实现您的所有功能;只是一个潜在解决方案的快速而肮脏的例子。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 2011-09-14
    • 2020-11-17
    相关资源
    最近更新 更多