The following code just wouldn't compile:
template< typename Fn >
bool templateFunctionOne( Fn&& fn )
{
int v = 5;
return fn( v );
}
template < typename Fn >
bool templateFunctionTwo( Fn&& fn )
{
std::future< bool > tk( std::async( std::launch::async,
&templateFunctionOne< Fn >,
std::forward<Fn>(fn ) ) );
return tk.get();
}
bool printThis( int value )
{
cout << value << endl;
return true;
}
int main()
{
auto func = std::bind( &printThis, std::placeholders::_1 );
return templateFunctionTwo( func );
}
when compiling it gives the following error:
functional::1665:61: error: no type named 'type' in 'class std::result_of< bool ((std::_Bind(std::_Placeholder<1>))(int)>))(std::_Bind))(int)>&)>'
I simplified the above, still it won't compile with the same error message:
template< typename Fn >
bool templateFunctionOne( Fn&& fn )
{
return fn();
}
template < typename Fn >
bool templateFunctionTwo( Fn&& fn )
{
std::future< bool > tk( std::async( std::launch::async,
&templateFunctionOne< Fn >,
std::forward<Fn>(fn ) ) );
return tk.get();
}
bool printThis()
{
return true;
}
int main()
{
auto func = std::bind( &printThis );
return templateFunctionTwo( func );
}
Since now the function printThis doesn't require passing in any parameters, binding the call isn't necessary. And when changing the call in the main as follows, it compiles fine:
int main()
{
return templateFunctionTwo( &printThis );
}
Could someone help to explain? I've seen the same kind of errors when passing a function pointer without using std::ref to wrap a parameter when its reference is required, but this seems to be something else (or not), what am I missing here?