syntax for using function pointers in C++

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

syntax for using function pointers in C++

John W. Eaton
Administrator
When calling a function using a function pointer we sometimes use the C
syntax

   (*fptr) (...);

and sometimes the syntax allowed by C++

   fptr (...);

is there any consensus on what is the best style?  I'd like for us to
consistently use one or the other.

Or is there something else we should be doing with modern C++ instead of
function pointers (though I'm afraid we may be stuck on this one in many
cases because of backward compatibility reasons)?

Other Comments?

Preferred bikeshed colors?

jwe

Reply | Threaded
Open this post in threaded view
|

Re: syntax for using function pointers in C++

John Swensen-3

On Oct 21, 2015, at 9:11 AM, John W. Eaton <[hidden email]> wrote:

When calling a function using a function pointer we sometimes use the C syntax

 (*fptr) (...);

and sometimes the syntax allowed by C++

 fptr (...);

is there any consensus on what is the best style?  I'd like for us to consistently use one or the other.

Or is there something else we should be doing with modern C++ instead of function pointers (though I'm afraid we may be stuck on this one in many cases because of backward compatibility reasons)?

Other Comments?

Preferred bikeshed colors?

jwe


I have seen a few projects start to use std::function (available since C++11). I haven’t used it myself, but when I first saw it used I went searching for pros & cons versus function pointers. I found the following StackOverflow article at the time which described them well.


John S.