C++: Function Pointer to Functions with Variable Number of Arguments

C++: Function pointer to functions with variable number of arguments

You could use std::function<> and std::bind().

#include <functional>

using std::placeholders::_1;

typedef std::function<bool(MyClass&)> my_fun_t;
my_fun_t my_fun;

if (condition1)
my_fun = std::bind(&MyClass::function_one, _1);
else if (condition2)
my_fun = std::bind(&MyClass::function_two, _1, a, b);
else if (condition3)
my_fun = std::bind(&MyClass::function_three, _1, a, b, c);
else if (condition4)
my_fun = std::bind(&MyClass::function_four, _1, a, b, c, d);

while (my_fun(my_class)) { ... }

These assumes you will use C++11. If you can't use C++11 but can use TR1, replace all std:: with std::tr1::. There is also a Boost implementation.

Pointer of a function with variable number of arguments

Functions are pointers naturally. So you can simply call:

funcPtr(3,3,2,5);

It looks like you have everything right. If the function does not have variable arguments, it is a really good idea to declare the function pointer with the right "shape" of arguments for protection from passing malformed arguments.

Function pointer to different functions with different arguments in C

My question is, for this scenario, Can I at all define a function pointer?

No. (Other than by dirty typecasting.)

Is there any other way to do this?

Your best bet is to create a wrapper function for one of your existing functions. For example:

double my_func_one_wrapper(double x, double p[], double c) {
return my_func_one(x, p[0], p[1], c);
}

That way, you have two functions with the same signature, and therefore the same function-pointer type.

Passing a function pointer with it's own parameters in C

Parameters are not passed. It is arguments that are passed.

So this function declaration

void runner(void (* function)(int in)){
(*function)(in);
}

has only one parameter: a pointer to a function, But if you want to call the pointed function that expects an argument then you need to supply an argument.

In this declaration of a function pointer

void (* function)(int in)

the function parameter in has the function prototype scope..

You may declare the function parameter without its identifier like

void (* function)(int)

So you have to declare the function with two parameters like

void runner(void (* function)(int), int in ){
function(in);
}

Pay attention to that to dereference the pointer to function is redundant.

All these calls as for example

( *function )( int );

or

( *****function )( in );

are equivalent to

function( in );

C - struct with a function pointer, how to fix variable number of arguments warning?

I don't quite get your concern, if it's warning you're after, then -Wno-incompatible-pointer-types is probably ok.

Else, if you want to suppress warnings selectively, then change func declaration to:

struct bigstruct {
void (*func) ();
};

, then func will accept various function pointers. Type void ()() in C is "function which takes unknown number of unknown arguments (and returns nothing)", unlike void()(void) which is "a function that takes no arguments".

C++ Call function pointer with any amount of variables

You can do it...

#include <iostream>

int f(int a, int b, bool c)
{
return c ? a + b : a - b;
}

template <typename R, typename... Args>
R callFunctionUsingMemAddress(void* funcaddr, Args... args)
{
typedef R(*Function)(Args...);
Function fnptr = (Function)funcaddr;
return fnptr(args...);
}
int main()
{
std::cout << callFunctionUsingMemAddress<int>((void*)f, 42, 10, true) << '\n';
std::cout << callFunctionUsingMemAddress<int>((void*)f, 42, 10, false) << '\n';
}

That said, be very careful: if the function you're calling expects say a double and you pass an int, there will be no checks or conversions performed as for a normal function call. You could add type safety to the code above easily if you haven't employed type erase over the function type, but I'm guessing you will have.

Resolve variadic argument to function pointer

The issue is func is being treated as a function pointer, but pointers to template functions are not allowed in C++.

You need to specify the type you plan to use when you reference func, like:

result = usefunc(0, func<int>);

You can use decltype to reference the type of a variable, to be a little more flexible:

result = usefunc(0, func<decltype(result)>);


Related Topics



Leave a reply



Submit