Thread: Pointers to function of any type question

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Arrow Pointers to function of any type question

    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.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    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

  4. #4
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by oogabooga View Post
    You'd have to use a variable argument list. See the header cstdarg.
    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.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Programmer_P View Post
    Ok, I looked at that, but it looked like its only for cases where the variable argument list follows a normal argument list
    Can you give an example of how you'd like to use the template function?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    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

  7. #7
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by oogabooga View Post
    Can you give an example of how you'd like to use the template function?
    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.

  8. #8
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by phantomotap View Post
    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.
    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.
    You asked for help with the core design, but you refused the excellent advice many regulars here discussed with you.
    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 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.
    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.
    But like noted earlier, I decided against using it due to the insanely high price.
    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
    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.
    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.

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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

  10. #10
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    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

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Programmer_P View Post
    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.
    But like noted earlier, I decided against using it due to the insanely high price.
    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?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question on function pointers
    By Durango2011 in forum C Programming
    Replies: 2
    Last Post: 12-04-2011, 12:52 PM
  2. Question on Function Pointers
    By rebelstar in forum C Programming
    Replies: 5
    Last Post: 09-26-2011, 09:42 PM
  3. pointers to function question
    By -EquinoX- in forum C Programming
    Replies: 31
    Last Post: 04-29-2008, 01:06 PM
  4. Question about my trim function and pointers
    By space-oddity in forum C Programming
    Replies: 10
    Last Post: 04-28-2008, 01:22 AM
  5. Strange type incompatibility using function pointers
    By Desolation in forum C++ Programming
    Replies: 3
    Last Post: 07-20-2006, 09:08 PM