Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create reference to function? [duplicate]

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 646
GlobalKiller Avatar asked Jan 26 '09 15:01

GlobalKiller


People also ask

What is the difference between reference and duplicate?

In simple terms, Duplicate will duplicate the code of the query while Query Reference will only refer the result of the query. Duplicate is generally used when you would like to create a similar query and you do not want to type the same code.

What is the difference between copy and duplicate in power query?

No difference! Duplicate copies a query with all the applied steps of it as a new query; an exact copy. After creating the copy, then you can go to the source step to change the URL: Using Duplicate, you managed to copy a query with all steps in it, and then make changes in your new query.

How do you reference a function in JavaScript?

Pass by Reference: In Pass by Reference, Function is called by directly passing the reference/address of the variable as an argument. So changing the value inside the function also change the original value. In JavaScript array and Object follows pass by reference property.

What is copy by value and copy by reference in JavaScript?

When you copy an object b = a both variables will point to the same address. This behavior is called copy by reference value. Strictly speaking in Ruby and JavaScript everything is copied by value. When it comes to objects though, the values happen to be the memory addresses of those objects.


3 Answers

Function references, unlike function pointers, make it harder to create them from an invalid source. This is useful if you are making a wrapper around a C library - the C++ code can take a callback function by reference and pass the pointer to the C library if the lbrary requires that the passed pointer must not be NULL.

It is also a convenient way to alias a function, especially in C++11 with the new auto keyword:

#include <iostream>
#include <typeinfo>

void f(int i, char c)
{
    std::cout << i << ' ' << c << std::endl;
}

int main()
{
    std::cout << typeid(f).name() << std::endl; //FvicE
    f(0, '1');

    void (*pf)(int, char) (&f); //ugly
    std::cout << typeid(pf).name() << std::endl; //PFvicE
    (*pf)(2, '3');
    pf(4, '5'); //works, but I don't recommend it

    void (&rf)(int, char) (f); //still ugly
    std::cout << typeid(rf).name() << std::endl; //FvicE
    rf(6, '7');

    auto &af (f); //pretty, but only works in C++11
    std::cout << typeid(af).name() << std::endl; //FvicE, same as above
    af(8, '9');
}
like image 84
LB-- Avatar answered Nov 15 '22 20:11

LB--


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 34
Robert Gould Avatar answered Nov 15 '22 19:11

Robert Gould


in addition to the use as strategy (as pointed out by Robert Gould), I freqently use them at the entrance point to (template) metaprogramming. A function reference can easily be picked up by a template parameter; from this point on it can be passed through several layers of (metaprogramming) templates. Of course, this holds true for a function pointer as well, but the reference is an alias and thus communicates the intention more clearly.

To give an example: when writing a generic command dispatching system for an application, a lot of different operations need to be announced as commands. We can use a simple "builder function" as front-end for the client code. Behind the scenes, this builder function picks up the actual function signature as template parameter, derives (by template metaprogramming) the actual parameter and return type values and possibly picks the suitable specialisation to store a "memento" and an "undo functor". These functors can than be stored either as function pointers internally, or using boost or tr1 or C++11 function objects. This way, it is possible to build a type safe command invocation and "undo" system.

like image 24
Ichthyo Avatar answered Nov 15 '22 19:11

Ichthyo