
on Tue Apr 29 2008, Anthony Williams <anthony_w.geo-AT-yahoo.com> wrote:
David Abrahams <dave@boost-consulting.com> writes:
Now I just ran into another issue, from the other side of the result_of interface. If I have a function template taking a function object argument by reference, and I want to use result_of with it, I need to do something special to account for the case where I've been passed a function reference. To wit:
template <class F> typename result_of<F()>::type call(F const& f) { return f(); }
int f() { return 0; }
int x = call(f);
The problem is that in the above case, F is deduced as int(), and we form an illegal function type (one that itself returns a function type) in result_of<F()>::type.
Is there an easier way?
In cases like this, I've often just overloaded call():
template<typename R> R call(R(*f)()) { return f(); }
If you've got function call arguments to deal with, it's not so easy, as you can end up with a lot of overloads.
Yeah, and it doesn't work if you're really defining class templates instead of simple functions because there you won't get the usual decay to function pointer types. -- Dave Abrahams Boost Consulting http://boost-consulting.com