Kod kopiluje sie tylko w pewnych wersjach g++

0
 
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 ??

0

To błąd kompilatora. Standard jedyne co określa to to, że podany funktor nie powinien modyfikować przekazanych obiektów - It is assumed that comp will not apply any non-constant function through the dereferenced iterator.[25.4.2] Tym, niemniej lepiej dodawać const wszędzie tam, gdzie można.

0

Dzięki ;)

1 użytkowników online, w tym zalogowanych: 0, gości: 1