S
Steve Hicks
Hi,
I'm using g++-4.3.3 and trying to compile the following minimal code
#include <tr1/functional>
using std::tr1::function;
int foo(int x) { return x+1; }
template <typename T> T bar(function<T(int)> f,int a) { return f(a); }
int main() { return bar(foo,1); }
and I get the error "no matching function for call to ‘bar(int (&)
(int), int)". It works if I change bar to take a function<int(int)>.
It also works if I add an explicit cast, as in bar((function<int(int)>)
foo,1). And I know the constructor is implicit, since I can say
"function<int(int)> foo_f = foo; bar(foo_f,1);" and it works just
fine. My code also fails when using a function object (with a member
int operator()(int)).
But I'm trying to make a function similar to bind, where you can pass
any sort of function reference/pointer/object/etc without any explicit
casts, and it "just works" (in fact, I'd rather take a function<T(A)>
with both types templated, but one thing at a time...)
I looked at gcc's tr1_impl/functional header and see that function
objects and bind take all sorts of scary-looking variadic templates,
and the entire functor input is a single template _Functor, rather
than an explicit function<T(A)>. But since function<> does have these
implicit constructors, is there any way to help out the compiler in
figuring out what's so obvious to a human? It seems like matching the
argument types is much more straightforward if I can just specify the
same A in both arguments bar(function<T(A)>,A)... I wouldn't even know
how to start making sense of it when the functor and the argument
types are separate templates. How can I go about doing that, if it's
actually necessary?
Thanks,
Steve
I'm using g++-4.3.3 and trying to compile the following minimal code
#include <tr1/functional>
using std::tr1::function;
int foo(int x) { return x+1; }
template <typename T> T bar(function<T(int)> f,int a) { return f(a); }
int main() { return bar(foo,1); }
and I get the error "no matching function for call to ‘bar(int (&)
(int), int)". It works if I change bar to take a function<int(int)>.
It also works if I add an explicit cast, as in bar((function<int(int)>)
foo,1). And I know the constructor is implicit, since I can say
"function<int(int)> foo_f = foo; bar(foo_f,1);" and it works just
fine. My code also fails when using a function object (with a member
int operator()(int)).
But I'm trying to make a function similar to bind, where you can pass
any sort of function reference/pointer/object/etc without any explicit
casts, and it "just works" (in fact, I'd rather take a function<T(A)>
with both types templated, but one thing at a time...)
I looked at gcc's tr1_impl/functional header and see that function
objects and bind take all sorts of scary-looking variadic templates,
and the entire functor input is a single template _Functor, rather
than an explicit function<T(A)>. But since function<> does have these
implicit constructors, is there any way to help out the compiler in
figuring out what's so obvious to a human? It seems like matching the
argument types is much more straightforward if I can just specify the
same A in both arguments bar(function<T(A)>,A)... I wouldn't even know
how to start making sense of it when the functor and the argument
types are separate templates. How can I go about doing that, if it's
actually necessary?
Thanks,
Steve