How can I boost::bind()
a template function?
I want this code (inspired by the boost::bind
bind_as_compose.cpp
example) to compile and run. Note the evaluation is different than in the bind_as_compose.cpp
example; fff()
begins running before kkk()
:
template<class F>
void fff(F fun)
{
std::cout << "fff(";
fun();
std::cout << ")";
}
void kkk()
{
std::cout << "kkk()";
}
void test()
{
fff(kkk); // "Regular" call - OK
// bind(fff,kkk)(); // Call via bind: Does not compile!!!
}
To print:
fff(kkk())
fff(kkk())
Update: Based on this answer, I got this to work:
void (&fff_ptr)(void(void)) = fff;
boost::bind(fff_ptr, kkk)();
However, this requires me to explicitly specify the instantiation types, which kinds beats the purpose...
Update 2
Ultimately, I wanted to pass the bound object as a nullary callable-type argument to another function like fff()
. In this case, what would be the explicit types?
Say I have another template function ggg()
:
template<class F>
void ggg(F fun)
{
std::cout << "ggg(";
fun();
std::cout << ")";
}
How can I use bind to get this output: fff(ggg(kkk()))
?
This does not seem to work:
boost::bind(fff<void()>, boost::bind(ggg<void()>, kkk))();