Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass function by value (?) instead of function pointer?

Sorry if this has been asked before, but I was unable to find it.

So im trying to educate myself about templates and the new C++11 features (mainly lambdas, something I always liked in other languages).

But in my tests I came to something I had no idea it worked, and I'm trying to understand how it works but cant figure it out..

The following code:

template <class Func>
void Test( Func callback ) {
    callback( 3 );
}

void Callback( int i ) {
    std::cout << i << std::endl;
}

int main( int argc, char** argv ) {
    Test( &Callback ); // this I was expecting to work, compiler will see its a pointer to a function
    Test( Callback ); // this also works, but how?!
    return 0;
}

If I understand how templates work, basically they're a scheme for the compiler to know what to build, so the first call Test( &Callback ); I was expecting to work because the compiler will see the template receives a function address and will assume the arguments should be a pointer.

But what is the second call? What is the template assuming it is? A copy of a functio (if that even makes any sense)?

like image 786
sap Avatar asked Aug 18 '11 17:08

sap


People also ask

What happens when you pass a pointer to a function?

In this example, we are passing a pointer to a function. When we pass a pointer as an argument instead of a variable then the address of the variable is passed instead of the value.

What is the difference between passing arguments by value and pointer?

This is the benefit of passing the arguments by value. However, when the arguments are passed on by pointers, the actual parameters are passed on to the function. The function gets the values from the actual addresses of the parameters. Any change in the values of arguments by the function gets stored at the addresses of parameters.

Are pointers passed by value or by reference in C++?

5 Answers 5. Yes to both. Pointers are passed by value as anything else. That means the contents of the pointer variable (the address of the object pointed to) is copied. That means that if you change the value of the pointer in the function body, that change will not be reflected in the external pointer that will still point to the old object.

Why do we use pointers in C++ instead of arguments?

This is because the function gets only copies of the values of parameters; it may change the copies but cannot change the variables because the function does not know where they are stored. Pointers keep the actual addresses of variables; therefore, passing arguments through pointers is as good as passing on the variables to the function.


1 Answers

A function is implicitly convertible to a pointer to itself; this conversion happens pretty much everywhere. Test(Callback) is exactly the same as Test(&Callback). There is no difference. In both cases, Func is deduced to be void(*)(int).

Function pointers are weird. You can find out more about them in "Why do all these crazy function pointer definitions all work?"

like image 140
James McNellis Avatar answered Oct 11 '22 13:10

James McNellis