I have the following C code :
inline void ChooseNext ( std::vector<RLS> & rls_vec_left_to_choose_,
int & num_files_left_to_choose_, const int algo_,
std::vector<RLS> & rls_vec_chosen_ )
{
if ( ( num_files_left_to_choose_ > 0 )
&& ( rls_vec_left_to_choose_.size ( ) > 0 ) )
{
switch ( algo_ )
{
case kAlgo1 :
std::sort ( rls_vec_left_to_choose_.begin ( ), rls_vec_left_to_choose_.end ( ), SortFunc1 );
break;
case kAlgo2 :
default:
std::sort ( rls_vec_left_to_choose_.begin ( ), rls_vec_left_to_choose_.end ( ), SortFunc2 );
break;
}
// etc
}
}
Where the sort functions are all of the type :
bool SortFunc1 ( const RLS & d1, const RLS & d2 ) ;
How do I change it to a function that takes const int algo_ and returns a bool (*) ( const RLS & d1, const RLS & d2 ) and then remove the switch case in this function ?
I am trying to do it in as readable a manner as possible, and hopefully using C++11 features.
SortFuncX. This might impact your performance, so measure after changing it.std::functionwhen compared to the solution you currently have; using a g++4.8.1 and a clang++3.4