[function] template deduction issues

Say I have a function as follows: *template< typename t_packet > void Subscribe( boost::function<void (t_packet const&)> func ) { // gSignal is a boost::signal somewhere. Assume valid. gSignal.connect( func ); }* And in some other function, I invoke Subscribe() as follows: * void MyCallback( WalkPacket const& p ) { } void MainTest() { Subscribe( &MyCallback ); }* The code above will actually not compile, since the template parameter t_packet cannot be deduced. Is there a way I can make the template deduction work?

On Fri, Apr 25, 2008 at 4:09 PM, Steven Watanabe <watanabesj@gmail.com> wrote:
That kind of defeats the purpose. I'm trying to make Subscribe figure out the type of the packet itself to avoid having to explicitly say "I'm subscribing for a WalkPacket" when the signature of the slot already has this information. The current implementation requires this syntax: Subscribe<WalkPacket>( &walkPacketCallback );

AMDG Robert Dailey wrote:
There is no way to deduce what a function object can be called with since it can have an overloaded operator(). To deal with monomorphic unary function objects that follow the std library conventions, use this: (warning untested.) BOOST_MPL_HAS_XXX_TRAIT_DEF(argument_type); template<class T> typename boost::enable_if<has_argument_type<T> >::type Subscribe(const T& t) { Subscribe(boost::function<void(const typename boost::remove_cv<typename boost::remove_reference<typename T::argument_type>::type>::type&)>(callback)); } In Christ, Steven Watanabe
participants (2)
-
Robert Dailey
-
Steven Watanabe