Home > OS >  How i colud use template function in arguments of template function?
How i colud use template function in arguments of template function?

Time:10-07

I'm trying to realize some abstraction with functions in c .

I want to do template function which takes two functions as arguments:

template <class inpOutp, class decis>
    bool    is_part_of_triangle(inpOutp ft_take_data,
                                decis ft_result){
        return (ft_take_data(ft_result));
    }
  • first one ft_take_data is template too and takes one function as argument:
template <class dec>
    bool    take_data(dec ft_result){
        ...
        ft_result(cathetus_size, x_X, y_X);
        ...
    }
  • second one ft_result should be the argument of ft_take_data:
int result(int cath_size, int x_X, int x_Y){
    ...
}

And i try to run it all in main like:

int main(void){
    return (is_part_of_triangle(take_data, result));
}

But i have the error from compiler:

 error: no matching function for call to 'is_part_of_triangle(<unresolved overloaded function type>, int (&)(int, int, int))'
  return (is_part_of_triangle(take_data, result));

main.cpp:38:7: note: candidate: template<class inpOutp, class decis> bool is_part_of_triangle(inpOutp, decis)
  bool is_part_of_triangle(inpOutp ft_take_data,
       ^~~~~~~~~~~~~~~~~~~
main.cpp:38:7: note:   template argument deduction/substitution failed:
main.cpp:49:47: note:   couldn't deduce template parameter 'inpOutp'
  return (is_part_of_triangle(take_data, result));

How can i realize this scheme - run template function with two functions in arguments, one of which the template function too (which call second one):

-> func1(func2, func3);

-> in func1 { func2(func3); }

-> in func2 { func3(...); }

CodePudding user response:

The take_data is a template not an real function of which the address/ function pointer can be passed. In order to get a concrete function, the template must be instantiated.

That means you need to pass something like:

take_data<TYPE OF NON-TEMPLATE FUNCTION>

Or simply

take_data<decltype(FUNCTION)>

That means you can either

return is_part_of_triangle(&take_data<int (*)(int, int, int)>, &result);

Or

return is_part_of_triangle(&take_data<decltype(result)>, &result);

CodePudding user response:

When take_data is a template function, you must specify its template when you pass this function as a parameter

You can do it like this:

typedef int(*RESULT_FUNC)(int, int, int);
return (is_part_of_triangle(&take_data<RESULT_FUNC>, &result));
  • Related