Pass a template method as an argument

前端 未结 3 810
后悔当初
后悔当初 2021-01-22 03:58

Could some one help me how to implement this code?

I need to pass a function to another function:

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


        
3条回答
  •  感情败类
    2021-01-22 04:46

    This is the closest to your code that compiles:

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

提交回复
热议问题