5 years, 4 months ago.

How do you interpret "(*(F *)p)(a0, a1, a2, a3);"

It's in callback.h (https://os.mbed.com/docs/v5.10/mbed-os-api-doxy/_callback_8h_source.html) around line 3157.

Here's the entire template containing the text

template <typename F> static R function_call(const void *p, A0 a0, A1 a1, A2 a2, A3 a3) { return (*(F *)p)(a0, a1, a2, a3); 3158 }

I've never seen a construct like "(*(F *)p)" nor do I see documentation of that construct.

Wow! It didn't occur to me that the "(F *) is a cast. Thanks!

My new puzzle is where are the types A0, A1, ... are defined. I'll be looking above the excerpt I gave in the huge .h file.

posted by Alfred Hume 03 Dec 2018

1 Answer

5 years, 4 months ago.

Hello Alfred,

Let's assume p is a pointer to a function which takes four parameters. Then in order to call that function we should dereference p as follows:

 (*p)(a0, a1, a2, a3);  // Dereference calls the function which 'p' is pointing to

If p is a void pointer but we know that it's pointing to a function which is of type F (that is taking four parameters), then in order to call that function we first have to cast pointer p to a pointer which is pointing to a F type function and then call that function by making a dereference:

(*(F *)p)(a0, a1, a2, a3);  // First cast 'p' to a pointer to a function of type 'F'. 
                            // Then call that function by dereference.

C-style casts (as the one used above) are difficult to find. C++ includes an explicit cast syntax which is easier to locate than the old standard cast:

(*static_cast<F*>(p))(a0, a1, a2, a3);  

Accepted Answer