MaiaVictor
MaiaVictor

Reputation: 52987

How to create a function that receives a function, calls it and returns it on C++?

I'm trying to create a function that receives a function, calls it and returns it back. I've tried several things already, including a lot of template combinations, an none seems to work. What is the correct way to do this?

Upvotes: 2

Views: 144

Answers (4)

Jerry Coffin
Jerry Coffin

Reputation: 490138

One function can't receive another function as a parameter -- C didn't allow it, and C++ doesn't either.

You can, however, pass a pointer to a function as a parameter, then invoke the function via that pointer.

#include <iostream>

int f() { std::cout << "called f\n"; return 2; }

typedef int (*ptr)();

int g(ptr p) { return p(); }

int main(){
    std::cout << g(f);
}

Result:

called f
2

If you're willing to use a function template instead of a function, you can pass the name of a function as a template parameter and invoke that. In most cases, it's preferable to use a functor (an instance of a class that overloads operator() so it can be invoked like a function) instead of a function though.

Upvotes: 5

Billy ONeal
Billy ONeal

Reputation: 106549

template <typename Functor>
Functor your_function(Functor toCall)
{
    toCall();
    return toCall;
}

If you want to return what the functor returns, then you'd use something like:

// Requires C++11
#include <type_traits>

template <typename Functor>
typename std::result_of<Functor()>::type your_function(Functor toCall)
{
    return toCall();
}

Also note that this will get easier in C++14 with decltype(auto):

//Requires C++14    
template <typename Functor>
decltype(auto) your_function(Functor toCall)
{
    return toCall();
}

Upvotes: 9

Eric Adem
Eric Adem

Reputation: 16

Here's the syntax for a function that is passed a function pointer as one of its parameters:

int Func (int (*funcToCall) (int, int)) /* This is how to receive the function as a parameter */
{
    int val = funcToCall (1, 2); /* This is how to call the function once it's received as a parameter */
    return (val);
}
int FuncToBePassed (int x, int y) /* This is a sample function that fits the format we need to pass into Func */
{
    return (x+y);
}
printf ("%d\n", Func (FuncToBePassed)); /* This is how to pass the function reference to the first function */

So, in this example, we have a function (FuncToBePassed) with two int parameters and then a function (Func) that expects to be passed a function like FuncToBePassed. Finally, the last line of code is doing just that, calling Func and passing it a reference to FuncToBePassed.

By the way, if you're not totally committed to using C++, I'd strongly recommend using C# instead, as details like passing functions around are MUCH better designed. So, if you're going to spend time mastering a language, go for the one that will be more practical. Unless you're maintaining some code that's already in C++, which is understandable... Hope I answered your question!

Upvotes: 0

Hrishi
Hrishi

Reputation: 7138

algorithm:

  1. make the function receive a parameter which is a function pointer.
  2. invoke the function using the function pointer.
  3. return the function pointer. Functions are not first-class objects in C and C++. So you will need to use pointers to functions.

Upvotes: 2

Related Questions