【问题标题】:Pass a template method as an argument将模板方法作为参数传递
【发布时间】:2015-04-23 17:21:04
【问题描述】:

谁能帮我实现这段代码?

我需要将一个函数传递给另一个函数:

std::cout << process_time(Model::method1) << std::endl;

该函数将函数作为模板类型获取并在对象上调用它

template <typename F>
double process_time(F algorithm)
{
    Model model;
    double time=0;
    do
    {
        // ...
        time += model.algorithm(arg1);
    } while (! stop_criteria);
    return time;
}

注意method1 也是一个函数模板:

template <typename T>
double method1(std::vector<T> &v)
{
    ...
}

它的合法语法是什么?

main.cpp

#include <iostream>
#include <vector>

class Model
{
public:

    template <typename T>
    double method1(std::vector<T> &v)
    {
        double t = 0;
        //...
        return t;
    }

    template <typename T>
    double method2(std::vector<T> &v)
    {
        double t = 0;
        //...
        return t;
    }

};

template <typename F>
double process_time(F algorithm)
{
    Model model;
    double time = 0;
    bool stop_criteria = false;
    do
    { 
        std::vector<int> arg1;
        // ...
        time += model.algorithm(arg1);
    } while (!stop_criteria);
    return time;
}

int main()
{
    std::cout << process_time(Model::method1) << std::endl;
    return 0;
}

【问题讨论】:

  • 什么是method1?你想做什么?你能提供一个MCVE
  • 什么是process_time
  • @anxieux 抱歉,已修复

标签: c++ templates c++11 argument-passing


【解决方案1】:

关键是method1是一个函数模板

template <typename T> double method1(std::vector<T>& );

因此,它不是 一个 函数……它是一个函数族。 process_time 需要一个函数,因此您只需将要使用的 one 函数传递给它:

std::cout << process_time(method1<int>) << std::endl;

【讨论】:

    【解决方案2】:

    这是最接近您编译的代码的:

    #include <iostream>
    #include <vector>
    
    struct Model {
      template <typename T>
      double method1(std::vector<T> &v) {
        double t = 0;
        //...
        return t;
      }
    };
    
    template <typename F>
    double process_time(F algorithm) {
        Model model;
        double time = 0;
        bool stop_criteria = false;
        do
        { 
            std::vector<int> arg1;
            // ...
            time += (model.*algorithm)(arg1);
        } while (!stop_criteria);
        return time;
    }
    
    int main() {
      std::cout << process_time(&Model::method1<int>) << std::endl;
    }
    

    【讨论】:

      【解决方案3】:

      将您的process time 行与time += 更改为:

         time += algorithm(&model,arg1);
      

      然后调用它:

      std::cout << process_time([](Model* m, std::vector<int>& v){return m->method1(v);}) << std::endl;
      

      或在 C++14 中:

      std::cout << process_time([](Model* m, auto& v){return m->method1(v);}) << std::endl;
      

      这将选择传递给method1 的向量类型留给process_time,而不是在调用站点修复它。

      基本上,这避免了处理指向成员变量的指针。相反,process_time 中的algorithm 只是从Model x vectordouble 的映射。这也意味着我们可以有一个非会员algorithm

      如果您不喜欢通话现场上面的冗长,您可以将更改保留为process_time,并将通话更改为:

      std::cout << process_time(std::ref(&Model::method1<int>)) << std::endl;
      

      as std::ref 接受一个指向成员函数的指针,并返回一个可调用对象,该对象将指向Model 的指针作为第一个参数,std::vector&lt;int&gt;&amp; 作为第二个参数。这符合我们的使用方式。

      【讨论】:

        猜你喜欢
        • 2018-11-24
        • 1970-01-01
        • 1970-01-01
        • 2013-11-08
        • 1970-01-01
        • 2011-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多