c++ function pointers

Started by
23 comments, last by Pink Horror 9 years, 9 months ago

Use http://www.codeproject.com/Articles/7150/Member-Function-Pointers-and-the-Fastest-Possible

Advertisement

I always found std::function to be the easiest way to deal with function pointers. The biggest benefit is the easy use of multiple types of function like objects, including lambdas (which I find surprisingly fun to use...). I think you will find the syntax easier to use, and it seems easier to understand than raw function pointers.

I have actually used function references too, for the same reasons you'd use references over pointers to variables.

"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

8 Years never needed a function pointer and I can't find a good example.

It is funny because in C++ you use them all the time behind the scenes. Ever used a virtual function? Ever sort something? Use a sorted container? Then you've used them.

A huge swath of the C++ standard library takes function pointers, but automatically converts them into objects (like std::function) that have already been covered. Under the hood or in the header files you will see those function pointers. Most of the containers take an optional comparison function pointer.

Whenever you do a comparison you end up with function pointers, although it is usually hidden from you. You pass a comparison function, often called 'less', which is just a fancy indirection for operator<(). Or in the case of strings, likely a pointer to the equivalent of strcmp().


The language's push to inline everything has hidden a lot of function pointers. It was heavy on the minds of even the C++98 standard. There is no way to really inline a function pointer, but if there is enough information for the compiler to figure out the function it will attempt to inline it. That was part of the big promise behind template functions when they were introduced. Lambdas are perhaps the penultimate form of this, with some common forms of template abuse as a close second. Lambdas, under an older compiler model, would be a function pointer to an anonymous structure's member function. The compiler can (but is not obligated to) treat the lambda expression as a block of code to be inlined instead of a pointer to branch and return from. If the compiler doesn't optimize it for some reason, either because it is in a different translation unit or in debug builds it likely will remain a function pointer. Using things like std::function or lambdas allows the compiler to POTENTIALLY inline the function, eliminating the costs of calling and returning from a function, and possibly eliminating a bunch of other work as well. By making these things that are transparently convertible to a function pointer the compiler has more options available to it. As the programmer in C++ you can learn to expose the information to the compiler.

The trade off is slower compile times that require enormous resources to build the code, but the benefit is that many functions can be completely or partially elided.

(It is one of many of C++'s performance-crushing habits that would be fatal except for the 'inline everything and optimize it into oblivion' compiler model. So much of that is to add a level of indirection to everything and then just inline it during compile. Many of the container classes have horrible performance when the are not inlined and then optimized to oblivion. The sad part is that new languages occasionally pick up the bad habits, like tiny accessors and mutators, but that probably belongs in a different discussion since they are not function pointers.)

Function pointers are there, and you use them indirectly even if you have forgotten about them. They are wrapped up and then used as a fallback when inlining is difficult or impossible for the compiler. You may see them by other names, but they are ever-present in C++.

Use http://www.codeproject.com/Articles/7150/Member-Function-Pointers-and-the-Fastest-Possible


How do fast delegates apply to this topic, which is about plain function pointers?

This topic is closed to new replies.

Advertisement