Re: passing functor or function to template class
On Tuesday, 10 July 2012 10:14:58 UTC-4, Christof Warlich wrote:
Hi,
I'm working at a generic library that must accept either
function pointers or functors to be passed to a template class.
This works quite straight forward as long as both are being
passed by value. But as the functors that need to be passed
may not be copyable, I'd like to pass functors by reference.
I think the correct approach is a bit different than your
solution.
- you should have a generic function wrapper (see std::function),
that is only concerned providing wrapper functionality on
a callable object.
- auxiliary facility to wrap non-copy-able objects ( see std::ref
and std::cref) by storing a reference - when possible.
Luckily C++11 (or Boost if you do not have C++11 compile) gives
you all the tools. See the modified version of your example:
#include <iostream>
#include <functional>
void *function()
{
std::cout << "function" << std::endl;
return 0;
}
struct Functor
{
private:
Functor( const Functor& );
public:
Functor(){}
void *operator()()
{
std::cout << "functor" << std::endl;
return 0;
}
} functor;
int main() {
std::function<void ()> vf = function;
std::function<void ()> vF = std::ref( functor );
vf();
vF();
std::function<void ()> rf = std::ref( function );
std::function<void ()> rF = std::ref( functor );
rf();
rF();
return 0;
}
Note: of course in this case you would not write
std::ref( function )
but it is possible, and will not yield to an error.
Ofc. you can replicate these tools if needed, and
by separating the concerns as described above,
should be able to get rid of the problematic
code-duplication you mentioned.
PS: Btw. I'd like to note that the usual definition of
functors have the requirement of assignable (http://www.sgi.com/tech/stl/Assignable.html), but of course
I accept that in some cases you might want to store it by
reference, but that probably indicate some design issues...
-- Zoltan