std::string type ="STRING";
std::string srt = "DESC";
static /*static - performance reasons*/ std::map<std::string, bool (*) (std::string&, std::string&)> sort_funcs = {
{"STRING_DESC", []( std::string& a, std::string& b)->bool { return std::greater<std::string>()(a,b); } } ,
{"STRING_ASC", []( std::string& a, std::string& b)->bool { return std::less<std::string>()(a,b); } },
{"INT_DESC", []( std::string& a, std::string& b)->bool { return std::greater<int>()(boost::lexical_cast<int>(a),boost::lexical_cast<int>(b)); } },
{"INT_ASC", []( std::string& a, std::string& b)->bool { return std::greater<int>()(boost::lexical_cast<int>(a),boost::lexical_cast<int>(b)); } },
{"UINT_DESC", []( std::string& a, std::string& b)->bool { return std::greater<unsigned int>()(boost::lexical_cast<unsigned int>(a),boost::lexical_cast<unsigned int>(b)); } },
{"UINT_ASC", []( std::string& a, std::string& b)->bool { return std::less<unsigned int>()(boost::lexical_cast<unsigned int>(a),boost::lexical_cast<unsigned int>(b)); } },
{"DOUBLE_DESC", []( std::string& a, std::string& b)->bool { return std::greater<double>()(boost::lexical_cast<double>(a),boost::lexical_cast<double>(b)); } },
{"DOUBLE_ASC", []( std::string& a, std::string& b)->bool { return std::less<double>()(boost::lexical_cast<double>(a),boost::lexical_cast<double>(b)); } },
};
std::sort(splitted.begin(), splitted.end(), sort_funcs[std::string(type + "_" + srt)]);
Powyższy kod kompiluje się kompilatorem g++ w wersji 4.9.2, zaś wersje starsze wywalają się z błędem:
/rtr/rtr/local/include/c++/4.8.1/bits/stl_algo.h:2287:35: error: invalid initialization of reference of type ‘std::basic_string<char>&’ from expression of type ‘const std::basic_string<char>’
while (__comp(*__first, __pivot))
^
/rtr/rtr/local/include/c++/4.8.1/bits/stl_algo.h:2290:34: error: invalid initialization of reference of type ‘std::basic_string<char>&’ from expression of type ‘const std::basic_string<char>’
while (__comp(__pivot, *__last))
Czy tobłędy, ktorejś wersji kompilatora czy mój ??