c++ - Using std::function and bind to assign functions with different argument lists -


i'm trying have function pointer where, in cases, either assigned function takes in 2 arguments (a cv::mat , struct contains parameters) or different function takes 3 arguments (the same 2 arguments , list of coordinates). figure std::function , std::bind i'm supposed using here.

mat process_f1(cv::mat img, feature_params f); mat process_f1_coords(cv::mat img, feature_params f, std::vector<std::pair<int, int> > feature_coords c); mat process_f2(cv::mat img, feature_params f); mat process_f2_coords(cv::mat img, feature_params f, std::vector<std::pair<int, int> > feature_coords ); // function pointer hold them std::function<cv::mat()> feature_func_f; //this how assign them: void set_feature_func(int feature_method, bool use_coords) {     switch (feature_method){     case 0:             if( !use_coords )                 feature_func_f = std::bind(process_f1,std::placeholders::_2);             else                 feature_func_f = std::bind(process_f1_coords,std::placeholders::_3);               break;         case 1:             if( !use_coords )                 feature_func_f = std::bind(process_f2,std::placeholders::_2);             else                 feature_func_f = std::bind(process_f2_coords,std::placeholders::_3);                break; } 

i intend call feature_func_f as:

cv::mat m, n; feature_params p; set_feature_func(0,false);  n = feature_func_f(m,p);  // or if have coordinate list c std::vector<std::pair<int, int> > c;  set_feature_func(0,true); n = feature_func_f(m,p,c); 

what doing wrong here? bunch of errors aren't meaningful in header functional:

error   4   error c2977: 'std::add_reference' : many template arguments c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional   900 1    error   5   error c2955: 'std::add_reference' : use of class template requires template argument list   c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional   900 1    error   6   error c2198: 'cv::mat (__cdecl *)(cv::mat,feature_params)' : few arguments call c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional   1149    1    error   2   error c2146: syntax error : missing ',' before identifier 'type'    c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional   900 1    error   3   error c2065: 'type' : undeclared identifier c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional   900 1    error   1   error c2027: use of undefined type 'std::tuple_element<0x01,_ftuple>'   c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional   900 1    

no, cannot do, want. std::bind binding arguments, shouldn't used in function call. can either use 2 functions, 1 2 params , 1 3 params, or send coords function set_feature_func.

just simple example usage of std::function , std::bind.

#include <functional> #include <iostream>  int main() {    std::function<void(int)> function;    auto l1 = [](int v) { std::cout << v << std::endl; };    auto l2 = [](int v1, int v2) { std::cout << v1 << " " << v2 << std::endl; };    function = l1;    function(5);    function = std::bind(l2, std::placeholders::_1, 10);    function(5); } 

and code should like

std::function<cv::mat(cv::mat, feature_params)> feature_func_f;  // coords vector<vector<...>> void set_feature_func(int feature_method, coords) {    // switch       if (coords.empty())       {          feature_func_f = process_f1;       }       else       {          feature_func_f = std::bind(process_f1, _1, _2, coords);       } } 

usage

std::vector<std::pair<int, int> > c;  set_feature_func(0,true,c); n = feature_func_f(m,p); 

Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -