6

Can we have a function pointer which points to a function which use a varying argument list? For example, lets say i need to select a function among a number of functions based on some input T 'input'. Can i use a STL map somthing like this??

template<typename T>
map<T, T (*)(T, ...)> func_map;

If this is possible can you tell me doing so will be right thing to do with design perspective.


[Edit] Actually i have a set of algorithmic functions and i have a message field. I need to select this algorithmic function based on few bytes values on this msg. I was thinking to use hash_map with a key value w.r.t. the pointer to that algorithmic function. i wanted to be very fast as per performance, what can be bast way to implement this.

Also, the reason why i am not selecting simple if-else or switch block for this is because the value which tell us the function that need to execute can refer to some other function at later point.

3
  • It just popped in my head. got curious and wi-fi so this question. srry didnt tried it yet :( Commented Sep 8, 2011 at 12:49
  • Note that there are no template typedefs, so what you wrote doesn't really exist, but in principle the answer is "yes". Commented Sep 8, 2011 at 12:53
  • @Marcelo - Thanks marcelo. didnt know there was a site that can run code so easily. Its a going to be my frequest bookmark. Commented Sep 8, 2011 at 12:58

2 Answers 2

3

Yes, it is possible to do this, but you probably shouldn't. There are a number of problems with variadic functions, including:

  • All types passed must be POD types or the behavior is undefined
  • There is no type safety whatsoever. If you pass the wrong type, you will cause undefined behavior.

I would recommend you find another approach. If you give more specifics on why you need to do this, perhaps someone can give you a better option.

Sign up to request clarification or add additional context in comments.

4 Comments

People keep telling me that I should write stuff like this std::cout << '(' << p.x << ', ' << p.y << ', ' << p.z << '): ' << type << std::endl; instead of like this printf("(%g, %g, %g): %s\n", p.x, p.y, p.z, type). I get the reasoning, but seriously, this is progress? The ... construct is hazardous and should be handled with heat-resistant forceps, but sometimes it just hits the spot.
C++0x might yield a very nice solution via user-defined literals. Something like "(%g, %g, %g): %s\n"F (note the F suffix) could resolve to a functor that takes four arguments of type double, double, double and const char*. Of course, this doesn't help with defining the OP's func_map. Indeed, I struggle to think of a viable alternative.
@MarceloCantos: Do you know about Boost Format?
@Daniel: Yes, and it is a distinct improvement over <<. but I want something that refuses to compile if my format string is wrong. Interestingly, conventional printf already exhibits this behaviour thank to very smart compilers.
0

Personally I think variable lists of arguments is a bad idea. It prevents you from using the compiler to do type checking and therefore make it easier to find bugs before any object code is produced.

I would therefore recommend that you find a way around this.

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.