I was wondering if is possible in C++ to write a templated function to accept pointers to functions of any type with any number of parameters?
If so, what would the function declaration look like?
Thanks in advance.
I was wondering if is possible in C++ to write a templated function to accept pointers to functions of any type with any number of parameters?
If so, what would the function declaration look like?
Thanks in advance.
I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.
You'd have to use a variable argument list. See the header cstdarg.
The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss
It would be a tremendously ugly arrangement of building a type list, unrolling that list, and forwarding.
That said, the question implies a fundamental design flaw. With a proper design, such a thing is almost always of limited use even where it is useful at all. (I have such a thing in my utilities library. I don't recall ever using it as anything more than a bandage while the interface of relevant components was expanded.) What are you trying to do? Do you realize that the client code will always require template behavior?
Soma
Ok, I looked at that, but it looked like its only for cases where the variable argument list follows a normal argument list, and not for cases where there's just a variable argument list (i.e. func_name(...), which I'm assuming is valid, of course, as I haven't actually tried using it before.).
I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.
I got to see the example before it was removed, and now I remember who you are.
Yea, your concept is fine, but your design is flawed. Your approach to parsing and generation design will continue to cause you design problems in other areas.
You asked for help with the core design, but you refused the excellent advice many regulars here discussed with you.
You thought you knew better than everyone then; I assume you will take that same approach now. However, I can tell you that what you are trying will never work the way you want it to work. The utility you are asking for solves the problem of storing, retrieving, and associating an interface in such a way as to bind functions with unrelated interfaces during build. That doesn't help you in any way. You want an object that can act as a client of an unknown interfaces during execution. (These have very little to do with one another.) It can't be done without generic message passing of some kind. Such a feature can be implemented with templates. That said, designing the transformation to be a client of a rule and a rule to be a client of the token stream is still the easiest approach.
Soma
Hold on a minute. I actually already posted an example, but after posting, I noticed an error in my logic that changed my view about the whole problem, so I deleted the post, and am changing my code a bit, and will post it in a second. I actually realized I don't need to use a variable argument list by itself, after all, as the functions which are being pointed to will have a parameter of type "target_type" which will be the type of the target of the rule, as the rule needs to know the target of itself, so when it is put into effect, it will operate upon that target object. Therefore, I can use cstdarg stuff after all, in cases when the rule start function has parameters after the target parameter.
I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.
I know my design is a little bit flawed, however I am aware about the areas it is flawed in, and its a work in progress. It is by no means completed yet. As to the last bit, that is your opinion...
I'll get it to work one way or another.
Yes..that is because they advised using strings, but I wanted to use enumerators because you would know at a glance looking at the class declaration what the available Html elements or tags or attributes were, and would ensure that you type it in correctly (btw, I got my ConvertEnumToStrings program, and the generated file it creates with the class which has the enum info in it, working).You asked for help with the core design, but you refused the excellent advice many regulars here discussed with you.
That may be what its main purpose is, but I know from talking to the company people themselves that Embedded Ch has a ch_eval function which functions like the Javascript "eval" function, and can execute C++ code inside of a string, which actually would help me.You thought you knew better than everyone then; I assume you will take that same approach now. However, I can tell you that what you are trying will never work the way you want it to work. The utility you are asking for solves the problem of storing, retrieving, and associating an interface in such a way as to bind functions with unrelated interfaces during build. That doesn't help you in any way.
But like noted earlier, I decided against using it due to the insanely high price.
That is in effect what I'm doing (or at least, the first part of what you said). The transformation function will be a client of the rule in the sense that the rule will know that it exists and how to call it, and will call it when appropriate.You want an object that can act as a client of an unknown interfaces during execution. (These have very little to do with one another.) It can't be done without generic message passing of some kind. Such a feature can be implemented with templates. That said, designing the transformation to be a client of a rule and a rule to be a client of the token stream is still the easiest approach.
Soma
Last edited by Programmer_P; 01-30-2012 at 10:34 PM.
I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.
After reading phantomotap's last post I see that this is far more complicated than what I was thinking of.
Still, if you end up using variable argument lists, they always have at least one definite parameter in order to give information about the number (at least) of the variable arguments. printf uses a string to define the number, type, and order of it's variable parameters. Nifty, but error prone.
But if I was you, I'd take the advice of the experts. They are, after all, only trying to help. And they do, believe it or not, know more than you.
The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss
I'm not certain exactly what it is you're trying to do but from what I gather Boost::FunctionTypes (and/or Boost::Fusion) would be of interest to you if you still want to go the templates route. It's possible to deconstruct (and construct) function pointers with FunctionTypes to get eg, the number and types of parameters. Boost::Fusion includes functionality for calling a function pointer with a tuple argument list, sort of like this example in Python.
Consider this post signed
Holy crap! Screw performance!
That is an insane security risk!
Browser makers have poured insane amounts of time into their Javascript engines to make them secure. Do you think you can do the same thing? I doubt it.
As for the question... won't just a functor do? I mean, so long as the function you pass in supports the required interface, you won't get a compile error.
Am I missing something?