How can I make unordered_set
with lambda? (I know how to make it with user defined hash struct and operator==
)
My current code is :
#include <unordered_set>
#include <functional>
struct Point
{
float x;
float y;
Point() : x(0), y(0) {}
};
int main()
{
auto hash=[](const Point& pt){
return (size_t)(pt.x*100 + pt.y);
};
auto hashFunc=[&hash](){
return std::function<size_t(const Point&)> (hash);
};
auto equal=[](const Point& pt1, const Point& pt2){
return ((pt1.x == pt2.x) && (pt1.y == pt2.y));
};
auto equalFunc=[&equal](){
return std::function<size_t(const Point&,const Point&)> (equal);
};
using PointHash=std::unordered_set<Point,decltype(hashFunc),decltype(equalFunc)>;
PointHash Test(10,hashFunc,equalFunc);
return 0;
}
It give me few! number of errors (live):
Note that I make a lambda for returning std::function
(equalFunc
,hashFunc
) because it seems that in unordered_set
some functions are trying to copy return type of that lambdas !
Also it's weird that gcc 4.8 compile that code fine ! ( live )