c++ - Passing my compar function to std::multiset with C++11 -
i have std::multiset, stores std::pair. want first attribute have no constraint on uniqueness, want second 1 unique. so, decided pass own function multiset, in order achieve (if not please let me know).
based on this answer, wrote similar function fails, , have no idea why (no idea of λ - , greek :) ).
auto f = [](std::pair<float, int>& a, std::pair<float, int>& b) { return (a.first < b.first && a.second != b.second); };
error:
error: expression ‘#‘lambda_expr’ not supported dump_expr#<expression error>’ not constant-expression sorry, unimplemented: non-static data member initializers error: unable deduce ‘auto’ ‘<expression error>’
i think cannot pass lambda (runtime construct) template parameter (compile-time construct). using struct operator()
works instead:
#include <set> struct my_compare { bool operator() (const std::pair<float, int>& a, const std::pair<float, int>& b) { return (a.first < b.first && a.second != b.second); }; }; int main(int argc, char ** argv) { std::multiset<std::pair<float, int>, my_compare> set; return 0; }
or, lambda , decltype (as in praetorian's answer):
#include <set> int main(int argc, char ** argv) { auto my_compare = [](const std::pair<float, int>& a, const std::pair<float, int>& b) { return (a.first < b.first && a.second != b.second); }; std::multiset<std::pair<float, int>, decltype(my_compare)> set(my_compare); return 0; }
Comments
Post a Comment