|
Boost Users : |
From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-01-04 08:47:01
From: "Duane Murphy" <duanemurphy_at_[hidden]>
> Is there a syntax for making boost::bind bind to functions that are
templates?
No. The reason is that a function template is not a function. It is an
entity that can generate functions.
You can't pass the name of a function template as an argument.
> Here is an (extremally simplified) example that doesnt compile:
>
> #include <boost/bind.hpp>
>
> template < class Function >
> void f( Function func )
> {
> func( 46 );
> }
>
> void g( int num )
> {
> num++;
> }
>
> int main( void )
> {
> boost::bind( f, g )();
> boost::bind( f, _1 )( g );
>
> return 0;
> }
>
> The two bind lines in main both get an error "call of non-function".
>
> Is there a way to make this work?
Yes, use a function object:
#include <boost/bind.hpp>
struct F
{
template < class Function > void operator()( Function func )
{
func( 46 );
}
} f;
void g( int num )
{
num++;
}
int main( void )
{
boost::bind<void>( f, g )();
boost::bind<void>( f, _1 )( g );
return 0;
}
-- Peter Dimov Multi Media Ltd.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net