0

I'm confused about this. When I am using a set and trying to use my custom comparison function, it shows:

C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_tree.h:457:21: error: static assertion failed: comparison object must be invocable as const
   static_assert(is_invocable_v<const _Compare&, const _Key&, const _Key&>,

My code is like this:

#include <iostream>
#include <vector>
#include <unordered_set>
#include <set>
#include <unordered_map>
#include <queue>
#include <algorithm>
using namespace std;
class mycomp{
public:
    bool operator()(const int& a, const int& b){
        return a > b;
    }
};
int main(){
    set<int, mycomp> test;
    test.insert(4);
    test.insert(5);
    test.insert(2);
    test.insert(1);
    test.insert(3);

}

I don't know why I have to add a const after the comparison function like this bool operator()(const int& a, const int& b) const{}.

Kevin eyeson
  • 375
  • 4
  • 8

0 Answers0