Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function References

Tags:

c++

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.

like image 289
GlobalKiller Avatar asked Jan 26 '09 15:01

GlobalKiller


People also ask

What is the difference between a function call and function reference?

Call by Reference: Both the actual and formal parameters refer to the same locations, so any changes made inside the function are actually reflected in actual parameters of the caller. While calling a function, we pass values of variables to it.

What is the purpose of the Excel function reference?

Using a formula to return a reference to a range of cells allows us to generate a reference on the fly based on the shape of the data or criteria we specify. As our data grows these formula generated references can automatically update to include new data.

What do you mean by function returning reference?

A C++ function can return a reference in a similar way as it returns a pointer. When a function returns a reference, it returns an implicit pointer to its return value. This way, a function can be used on the left side of an assignment statement.


2 Answers

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

like image 138
Robert Gould Avatar answered Oct 20 '22 01:10

Robert Gould


I think your example usage is quite good. Because if you would use an ordinary function 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).

like image 43
Johannes Schaub - litb Avatar answered Oct 19 '22 23:10

Johannes Schaub - litb