Why does gcc require explicit template arguments with my boost::bind

Hello, Below I implement a function "Chain" that takes two functions and executes them both. The first approach succeeds: direct function call. The second approach succeeds: call via bind with explicit template arguments The third and fourth approaches fail: call via bind without explicit template arguments (with/without boost::protect) Question: Why do I have to use explicit template arguments when using bind with Chain? Thank you, Chris === #include <boost/bind.hpp> #include <boost/function.hpp> template <typename TAFunc1, typename TAFunc2> void Chain(const TAFunc1& Func1, TAFunc2& Func2) { Func1(); Func2(); } void f() { } void foo() { boost::function<void()> x = boost::bind(f); boost::function<void()> y = boost::bind(f); Chain(x, y); boost::bind(Chain<boost::function<void()>, boost::function<void()> >, x, y)(); boost::bind(Chain, x, y)(); boost::bind(Chain, boost::protect(x), boost::protect(y))(); }

On 12/06/2012 06:34 AM, Chris Stankevitz wrote:
Hello,
Below I implement a function "Chain" that takes two functions and executes them both.
The first approach succeeds: direct function call.
The second approach succeeds: call via bind with explicit template arguments
The third and fourth approaches fail: call via bind without explicit template arguments (with/without boost::protect)
Question: Why do I have to use explicit template arguments when using bind with Chain? Chain is a template function. Its address isn't known until it gets instantiated. In order to bind a function, bind needs to know the address (aka which function to bind to). This means, that you have to specify the complete type.
Thank you,
Chris
===
#include<boost/bind.hpp> #include<boost/function.hpp>
template<typename TAFunc1, typename TAFunc2> void Chain(const TAFunc1& Func1, TAFunc2& Func2) { Func1(); Func2(); }
void f() { }
void foo() { boost::function<void()> x = boost::bind(f);
boost::function<void()> y = boost::bind(f);
Chain(x, y);
boost::bind(Chain<boost::function<void()>, boost::function<void()> >, x, y)();
boost::bind(Chain, x, y)();
boost::bind(Chain, boost::protect(x), boost::protect(y))(); } _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On 06/12/12 06:34, Chris Stankevitz wrote:
template <typename TAFunc1, typename TAFunc2> void Chain(const TAFunc1& Func1, TAFunc2& Func2) { Func1(); Func2(); }
Replace that by struct Chain_impl { typedef void result_type; template<typename TAFunc1, typename TAFunc2> result_type operator()(const TAFunc1& Func1, TAFunc2& Func2) const { Func1(); Func2(); } }; Chain_impl const Chain = {};

On Wed, Dec 5, 2012 at 9:34 PM, Chris Stankevitz <chrisstankevitz@gmail.com> wrote:
template <typename TAFunc1, typename TAFunc2> void Chain(const TAFunc1& Func1, TAFunc2& Func2) { Func1(); Func2(); }
Another problem with this: The second argument should be a const reference, not a mutable reference. Chris
participants (3)
-
Chris Stankevitz
-
Mathias Gaunard
-
Thomas Heller