0

I have a class which interpolates a 2D function automatically (quite happy with it). It accepts a dynamic function pointer to the method being interpolated. I have had to template out the class, since I need the object instance as well as the pointer.

Can I avoid templating the class out? Or will I need to keep doing so in order to accept the object instance as a parameter? Is a base class possible? (I'm fairly new to c++)

The way I have it is (abbreviated):

template<class F>
class Interpolate {
   Interpolate(double (F::*f)(double, double), F & obj, ...) { 
      ...
      double value = (object.*f)(x,y);
      ...
   }
}
Andy G
  • 19,232
  • 5
  • 47
  • 69
ccook
  • 5,869
  • 6
  • 56
  • 81

1 Answers1

1

As pointed out in the comment, you can also use std::function. This give you more flexibility with std::bind. On the other hand, std::function may allocate the resulting function on the heap and this causes a big overhead (which may be avoid by using std::cref() ). A very interesting discussion about the differences in execution time of std::function vs templates can be found at std::function vs template.

" Notice that std::function is able to store different types of callable objects. Hence, it must perform some type-erasure magic for the storage. Generally, this implies a dynamic memory allocation (by default through a call to new). It's well known that this is a quite costly operation. " by Cassio Neri - Here is where std::cref can help, specially because in the example discussed in the link I just wrote, without std::cref, the heap allocation of std::function slowed down the program by a factor 10!

Community
  • 1
  • 1
Vivian Miranda
  • 2,467
  • 1
  • 17
  • 27
  • Interesting, seems I get to choose between compile time and run time? – ccook Aug 30 '13 at 16:35
  • Not necessarily. With small functions, compiler may optimize the allocation ("The standard (20.8.11.2.1/5) encorages implementations to avoid the dynamic memory allocation for small objects which, thankfully, VS2012 does") and with larger functions, std::cref diminish the overhead considerably. It won't be as fast as templates (which is usually the rule - template solution is the fastest approach because work is done at compile time). This should be only critical if std::function wrap fast exec. functions and is called many many times (+- similar to virtual function overhead issue) – Vivian Miranda Aug 30 '13 at 16:46
  • How about with GCC (running in linux) when the class is loading windows DLLs for evaluation? The functions themselves are relatively expensive, so I'm trying to get a feel for where I would suffer the performance. On construction only would be fine - on eval, no. – ccook Aug 30 '13 at 16:49
  • Good Question. It should be easy to check. But one of the main point of that link is that if you don't pass the function by reference (using std::ref or std::cref), then the std::function overhead will be big even for expensive functions. That is a warning that I see few people being aware of. – Vivian Miranda Aug 30 '13 at 16:56