dimanche 25 décembre 2016

Difference between f and &f

Consider the code:

void f(int) {}

int main()
{
    std::function<void(int)>func = f;
    func(3);
    std::function<void(int)>funcc = &f;
    funcc(3);

    void(*ptr)(int) = f;
    ptr(3);
    void(*ptrr)(int) = &f;
    ptrr(3);
}

This code compiles successfully and it seems to me like all the calls of f are the same. In the first part function is being made a functor with wrapping in std::function and in the second part I'm using pointers to function.

My main question is: what is the difference between writing = f; and = &f;? Moreover, what is &f? Is it an address of f? Or a reference to f? I'll highly appreciate any explanations!

Aucun commentaire:

Enregistrer un commentaire