views:

83

answers:

1

Say I have a function f(a, b, c). Is it possible to create function pointers g and h such that when you use g and h they use a predetermined value for one of the arguments?

For example (*g)(b, c) would be equivalent to f(1, b, c) and (*h)(b, c) would be equivalent to calling f(2, b, c).

The reason why I am doing this is that I am calling a function to which I can obtain a pointer (through dlsym) and this function has drastically different behaviour depending on what the first argument is (so much so that they shouldn't really be called the same thing). What's more the function pointer variable that is being used in the main program can only generally take two arguments as this is a special case where the imported function has three (but really only two are necessary, one is essentially a settings parameter).

Thanks.

+3  A: 

I don't see why you can do this if you have defined the function g points to as

void gfunc(int b, int c) { 
  f (1, b, c); 
}
g = &gfunc;

The better way to do this in C++ is probably using functors.

Hemal Pandya
He means using default parameters, I guess.
Code Clown
+1 for functors. However the solution of Hemal is pretty straightforward and VolatileStorm can have his desired h and g.
Stephane Rolland
@Stephane: but the signature of the function pointers do not match anymore.
Code Clown
Code Clown I am confused. If g has to be called with lesser number of parameters than f than its signature cannot match that of f. There can be multiple function to which g can point, each of which can use a different default value (good reason to use functor, because each instance can use a different default :)). If their sig has to match that of f then they will need to ignore the first param which is not advisable.
Hemal Pandya
Whilst this works it is in fact the solution I already have in place, and isn't very "nice" (I'm a bit of a stickler for these things I admit). I'm not certain what is meant by "signature" but if I understand the desired general signature is that of g and h, f's is the one that needs to be forgotten about.
VolatileStorm
@VolatileStorm: What's not nice about it? Based on your description, it's unclear why you want function pointers at all. It sounds like you just want wrapper functions.
jamesdlin
Agree with jamesdin, it seems you need is different functions that directly call the common function using sensible values for the first parameter.
Hemal Pandya
I apologise for making it unclear, but it is necessary to have function pointers. This is also what makes it more difficult to have functions that directly call it with value.The function that g and h would be pointing to is in a dynamically linked library, so even to get at f without "preset" parameters I am using a function pointer.What is not nice is that I'm asking if this is possible to reduce the amount of code written and to make it clearer. It may not be simple to do this in C++, I suspect if it were C++0x though the addition of lambda functions would come in useful here.
VolatileStorm
@VolatileStorm: Making wrappers seems pretty clear to me, and the code is very minimal. I don't really see how it could be reduced even more.
jamesdlin
@VolatileStorm from what you have said so far I can see no reason to use lambdas here. Also I doubt that functions in dynamically linked library cannot be called without using function pointer. Please ensure you are no making this more complicated then it needs to be.
Hemal Pandya
@Hemal Pandya. What I meant by the use of lambdas is described here:http://stackoverflow.com/questions/152005/how-can-currying-be-done-in-c/160286#160286Also I am not making this more complicated than it needs to be, I'm trying to make it simple. I was hoping that there would be a cast of the form: g = (void(*)(2, int))f;Or something similar.On the topic of the dynamic linking, if you know of a way to call them without using a function pointer please enlighten me. It's the way that the dlfcn stuff works as far as I can tell.
VolatileStorm
@VolatileStorm ok I think understand now what you mean, and I still think creating new function(s) that call the original function is the best way to do it. I am not much of Windows developer but dlfcn seems to be useful for some reflection type of thing where the function names and/or library names are not known when writing client code. If you know the name of dll and the signature of the function to be called there has to be an easier way to do it.
Hemal Pandya
I'm a little confused, the dlfcn library is for linux. Windows uses farprocs as opposed to simple void pointers (if I remember correctly from my research). Also what do you mean by the names not being known. How can you load the symbols without knowing their names? In my case they aren't known at compile time, but they are known at runtime.
VolatileStorm
@VolatileStorm: Yes, I meant to say that the names are not when writing the client code.
Hemal Pandya