2

I'm trying to make a CPP function that takes a function pointer and an arbitrary number of arguments, adds some coordinates, then calls the function pointer as such:

int func_api(void(*fun)(int, int, ...args), ...args) {
    fun(this.x, this.y, args);
}

so I can use it as such:

int func(int x, int y, int param1, int param2) {
    something;
}

int foo () {
    func_api(p1, p2);
}

Is there a way to do this?

1

1 Answer 1

2

Use a variadic template

template <typename Func, typename ... Args>
int func_api(Func fun, Args&&... args)
{
 fun(this->x, this->y, std::forward<Args>(args)...);
}

You can now call it like this:

func_api(func, p1, p2);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.