tags:

views:

268

answers:

4

So I was just working with function pointers and I remembered that you could do this:

void Foo()
{
}

int main()
{
    void(& func)() = Foo;

    func(); //::Foo();
}

The obvious advantage being that references reference valid objects (unless they're misused), or functions in this case.

The obvious disadvantages being that you can't store an array of references and can't use them for member function pointers (at least as far as I can tell).

My question: does anyone use them (i.e., function references, not function pointers), and if so, in what scenarios have you found them useful/helpful?

The only place I can see them being useful off the bat is binding a reference to a certain function when working with conditional compilation.

+6  A: 

I've used them before to add customization to classes by passing them to the constructor in a way like the strategy pattern

Robert Gould
Ditto here, but I used boost::function instead. It's more flexible :)
Your input is appreciated.
GlobalKiller
Boost's solution is more flexible, but The solution I gave is good in embedded code where boost's overhead, on the compiled code. Is prohibitive.
Robert Gould
+2  A: 

I think your example usage is quite good. Because if you would use an ordinary fuction pointer, and you then apply the address-of operator, you would get the address of the function pointer. Using a reference to function will do the expected thing, in that it returns a pointer to the function itself.

I also can't think of many examples. Keeping function references, as you point out, has some ugly consequences. Another possibly unwanted consequence is, if kept as a class-member, your objects will be non-assignable if you don't write your own operator= and refrain from trying to re-assign the function-reference.

I think most uses of function references are implicit, much like most uses of array-references - although much more so, when you accept arguments by-reference:

template<typename T>
void do_something(T const& t) { ... }

While accepting arrays by reference has the advantage of not losing their size information, accepting functions by reference explicitly doesn't seem to have an advantage (at least as far as i can see). I suppose the existence of function references largely is justified by the idealistic view of a reference as an alias-name of some object or function, together with the fact that it allows passing functions to such templates that accept their argument by reference.

I would probably avoid using them if i wouldn't need them inevitably. Constant function pointers also provide non-reassignable callables, and will probably avoid confusions when other programmers, who possibly are not very familiar with this language niches, read your code. Worth to note that Vandervoorde & Josuttis also recommend to avoid them to reduce confusion (in their book C++ Templates - The Complete Guide).

Johannes Schaub - litb
Thanks for your answer.
GlobalKiller
Function pointers also have another advantage in that they can be made to look different from ordinary function calls: (*myFunc)(bar);
aib
aib. well the same is true for function references. you can even do (**************myFunc)(bar); it still works, because of the implicit conversion from function to function-pointer :) although i agree it looks funny
Johannes Schaub - litb
Hmm true, I did not think of that. You can do that with regular functions as well.
aib
+1  A: 

I've used them in a plug-in system where plug-in DLLs could be loaded/unloaded at run-time. I would look for known symbols in each DLL and cast them to function pointers.

Ates Goral
A: 

I was involved in a project that intended to implement an Object system in C using function pointers and structs to implement classes. Since the functions could be replaced, they could implement a sort of inheritance system. Fortunately, that effort broke down. While the task was interesting, it wasn't a good idea for the project (probably not a good idea for anything in production).

I've also used function pointers for a DLL based pluggable module system, as was mentioned before.

Sam Hoice