Sign in to follow this  

Can you improve this code?

This topic is 4842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Folks, I've written some code to handle and test the passing of pointers to template functions and I was hoping people with more knowledge (and sense) than I could offer some advice on improving it. I'm sure it's quite kludgy at the moment and that it could be improved, so please, if you have a few moments, have a crack at it (no, not at me... at the code! ;) ). It's quite short so it shouldn't take long! tfp.h tfp.cpp What's it supposed to do? Class foo is required to call a function, via a pointer, within foo's constructor. It needs to forward another function pointer as an argument in the first function call. However, foo and both of the functions (the called one and the forwarded one) are all template classes/functions. Additionally (and why function pointers are needed) is that there are different instantiations of the template functions that can be passed and this should be permissable as a run-time decision. I want to permit extensibility to allow for new split and cost functions to be provided later on and I don't want to have to change the foo class when a new function is added. Furthermore, cost and split should not be methods of foo. foo is ultimately an aggregated storage class (storing objects of the first template paramter, Type) and split and cost are functions to manipulate the stored objects. I've handled it using a wrapper class and polymorphism to the function templates that hide the function pointer. If there is a better way, please let me know. Having said that, I don't believe it needs a more generalised functor class (with variable template parameter lists). If you think I'm wrong about this, please let me know why. Note: The actual implementations of the Split and Cost functions are only intended as examples that show that the inteface works. Please don't base any decisions of the design on the content of these functions. Finally, any solution should have a neat and efficient interface. This is going to be used by certain idiot programmers I'm writing the code for and they have to be able to understand the interface (as do I ;) ). Thanks for any and all help. Cheers, Timkin

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
What part will be exposed to the client? foo? If only foo, why are you wrapping the function pointers inside functors? Is foo the only object that will invoke them? If so, how is wrapping the function pointer inside a functor, then passing the functor to foo any safer than just passing the function pointer to foo in the first place?

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
What part will be exposed to the client? foo?


Essentially foo only... although split and cost should be replaceable so their interface should conform to a given standard.

Quote:
Original post by Anonymous Poster
If only foo, why are you wrapping the function pointers inside functors?


They're not true functors (because they don't remember a state) but the are intended to permit the run-time choice of the functions to be passed to the constructor... as in the example I provided.

Quote:
Original post by Anonymous Poster
Is foo the only object that will invoke them?


Initially, yes... but that could change.

Quote:
Original post by Anonymous Poster
If so, how is wrapping the function pointer inside a functor, then passing the functor to foo any safer than just passing the function pointer to foo in the first place?


I wasn't suggesting it was safer. If you can write a clean and elegent way of passing around pointers to template functions that does exactly what I've written and has a simple interface, by all means, I'm all ears/eyes. That was the point of asking for advice... if someone has a better way, I'd love to hear it.

The problem as I saw it (and I could be wrong) is that you can't typedef a template function pointer, so since you don't know the signature of the function being passed until runtime, you either have to instantiate all possibilities or play around with templating. If I've missed something, please enlighten me.

Thanks,

Timkin

Share this post


Link to post
Share on other sites
Okay, I've figured out a much more succinct way of describing the problem...

I need to pass a function pointer to a class constructor. The function call (via the pointer) must forward another function pointer as an argument. The class is a template class. Both functions are template functions that depend on the template paramter passed to the constructor (plus other arguments whose type may vary since the functions can be user supplied).

If anyone can think of a better way of doing this than the code I posted above, please let me know.

(I do have a variant of the above that passes functor pointers as template parameters to the template class, meaning that I can hide all of the different combinations of split and cost functions with different template parameters behind a set of typedefs. Any better ideas?

Thanks,

Timkin

Share this post


Link to post
Share on other sites

This topic is 4842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this