--- At Fri, 4 Jan 2002 15:47:01 +0200, Peter Dimov wrote:
From: "Duane Murphy"
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.
I figured that was the problem.
Here is an (extremally simplified) example that doesnt compile:
#include
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
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; }
Thank you! That's the ticket. I also noticed that the function object can be unnamed which avoids any name collisions. The template can also be static. Is there any side affects to be concerned about that? What does it mean to have a static template function inside a struct? I have another related question. I have a situation where I want to use a bind object within a bind object but the bindings are unrelated. In the documentation it explains that the nested binds are treated specially, is there a way around this special treatment? With a few changes I think I can show the example (I didnt try and compile this). struct // F -- can be unnamed! { template < class Function > void operator()( const char* s, Function func ) { func( strlen( s ) ); } } f; void g( int a, int b ) { int sum = a + b; } int main( void ) { bind< void >( f, _1, bind( g, _1, 3 ) )( "example" ); return 0; } The _1 in the bind to f is different from the _1 for the bind to g. I believe there will be an error saying that const char* cannot be converted to int, because "example" is being bound to the first parameter of g. Thank you for your help. I am really enjoying to great capabilities that bind allows! ..Duane