[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?

AMDG Robert Dailey wrote:
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?
You can construct a boost::function at the call site explicitly rather than relying on the implicit conversion. Alternately you can provide an overload of Subscribe for function pointers. In Christ, Steven Watanabe

On Fri, Apr 25, 2008 at 4:09 PM, Steven Watanabe <watanabesj@gmail.com> wrote:
AMDG
Robert Dailey wrote:
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?
You can construct a boost::function at the call site explicitly rather than relying on the implicit conversion.
Alternately you can provide an overload of Subscribe for function pointers.
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:
You can construct a boost::function at the call site explicitly rather than relying on the implicit conversion.
Alternately you can provide an overload of Subscribe for function pointers.
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 );
If you only need to deal with function pointers, template<class T> void Subscribe(void (*callback)(const packet_t&)) { Subscribe(boost::function<void(const packet_t&)>(callback)); } In Christ, Steven Watanabe

On Fri, Apr 25, 2008 at 5:19 PM, Steven Watanabe <watanabesj@gmail.com> wrote:
AMDG
Robert Dailey wrote:
You can construct a boost::function at the call site explicitly
rather
than relying on the implicit conversion.
Alternately you can provide an overload of Subscribe for function pointers.
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 );
If you only need to deal with function pointers,
template<class T> void Subscribe(void (*callback)(const packet_t&)) { Subscribe(boost::function<void(const packet_t&)>(callback)); }
What about for function objects?

AMDG Robert Dailey wrote:
If you only need to deal with function pointers,
template<class T> void Subscribe(void (*callback)(const packet_t&)) { Subscribe(boost::function<void(const packet_t&)>(callback)); }
What about for function objects?
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