by arximboldi · 9y ago · 0 comments
std::function<void()> = [] { ... };
Vs: auto = [] { ... }
In the first case you are doing type erasure, which adds quite few penalties. Even in other cases, the type you typed might be convertible from the actual thing that is returned, causing extra conversions. If you always use "auto" the chances you use the right type and do less conversions is way higher.[1] https://github.com/mhogomchungu/tasks
[2] https://github.com/mhogomchungu/tasks/blob/a1512a1b5e0392a06...
template< typename T,typename ... Args >
future<T>& run( std::function< T( Args ... ) > function,Args ... args )
{
return Task::run<T>( std::bind( std::move( function ),std::move( args ) ... ) ) ;
}
It should look like this for that example to work without the type erasure (haven't tried, there might be typos): template< typename Fn,typename ... Args >
future<std::result_of_t(Fn(Args...))>& run( Fn&&, Args&& ... args )
{
return Task::run<std::result_of_t(Fn(Args...))>( std::bind( std::forward<Fn>( function ),std::forward<Args>( args ) ... ) ) ;
}
This version is also more efficient, since otherwise you are type erasing twice.[1] https://github.com/mhogomchungu/tasks/blob/4210a8fad57958fad...