Finding out a hidden typedef pointer

typedef solution_type (*algorithm_ptr_type) (
    problem_type problem,
    void (*post_evaluation_callback)(void *move, int score)/* = NULL*/
);

Please help me! Thanks you

+3
source share
4 answers

This means that it algorithm_ptr_typeis a pointer to a function that returns solution_typeand whose parameters are:

  • type problem problem_type
  • post_evaluation_callback, which again is a function pointer that takes two parameters ( void*and int) and returns void.

And the same can be written as ( easy and readable syntax ):

typedef  void (*callback_type)(void *move, int score);

typedef solution_type (*algorithm_type)(problem_type, callback_type);

Note: the parameter name is optional, so I deleted it to make typedef short and pretty!


In C ++ 11, this can be simplified as follows:

using algorithm_ptr_type = solution_type (*) (
    problem_type, 
    void(*)(void*, int)
);    

This is much better, since it is now clear what is being determined and in terms of what.


++ 11 ,

//first define a utility to make function pointer.
template<typename Return, typename ... Parameters>
using make_fn = Return (*)(Paramaters...);

,

using callback_type = make_fn<void, void*, int>;

using algorithm_type = make_fn<solution_type, problem_type, callback_type>;

make_fn - , - — !


:

solution_type SomeFunction(problem_type problem, callback post_evaluation)
{
   //implementation

   //call the callback function
   post_evaluation(arg1, arg2);
   //..
}

algorithm_ptr_type function = SomeFunction;

//call the function
function(arg, someOtherFunction);
+16

!

, , , algorithm_ptr_type, solution_type problem_type . void* int .

:

typedef void (*post_evaluation_callback)(void *move, int  score);
typedef solution_type (*algorithm_ptr_type)(problem_type problem, post_evaluation_callback callback);
+5

, algorithm_ptr_type .

, solution_type.
, :
0: problem_type.
1: , , : void.
:
0: A void*.
1: An int.

+2

solution_type , problem_type . void * (-) int .

+1

Source: https://habr.com/ru/post/1794631/


All Articles