Home > Back-end >  Static assertion failed error in defining set (STL container) with user defined objects c 17
Static assertion failed error in defining set (STL container) with user defined objects c 17

Time:06-07

I defined a set with data type as user defined object here.

#include<bits/stdc  .h>
using namespace std;
class triplets{
    public:
    int x,y,z;
    triplets(){

    }
    triplets(int x,int y,int z){
        this->x=x;
        this->y=y;
        this->z=z;
    }

};
class Cmp{
    public:
    Cmp(){};
    bool operator() (const triplets &a, const triplets &b){
        if( a.x == b.x){
            return a.y < b.y;
        }else{
            return a.x < b.x;
        }
    }
};
int main(){
    set<triplets,Cmp>s;
    s.insert(triplets(2,4,5));
    return 0;
}

The code compiles fine in c 11 and c 14 versions. But it doesn't compile in c 17 and above. It throws following error.

    In file included from /usr/include/c  /11/map:60,
                 from /usr/include/x86_64-linux-gnu/c  /11/bits/stdc  .h:81,
                 from Arrays/TwoPointer/test.cpp:1:
/usr/include/c  /11/bits/stl_tree.h: In instantiation of ‘static const _Key& std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_S_key(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Const_Link_type) [with _Key = triplets; _Val = triplets; _KeyOfValue = std::_Identity<triplets>; _Compare = Cmp; _Alloc = std::allocator<triplets>; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Const_Link_type = const std::_Rb_tree_node<triplets>*]’:
/usr/include/c  /11/bits/stl_tree.h:2071:47:   required from ‘std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_unique_pos(const key_type&) [with _Key = triplets; _Val = triplets; _KeyOfValue = std::_Identity<triplets>; _Compare = Cmp; _Alloc = std::allocator<triplets>; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::key_type = triplets]’
/usr/include/c  /11/bits/stl_tree.h:2124:4:   required from ‘std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = triplets; _Key = triplets; _Val = triplets; _KeyOfValue = std::_Identity<triplets>; _Compare = Cmp; _Alloc = std::allocator<triplets>]’
/usr/include/c  /11/bits/stl_set.h:521:25:   required from ‘std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(std::set<_Key, _Compare, _Alloc>::value_type&&) [with _Key = triplets; _Compare = Cmp; _Alloc = std::allocator<triplets>; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree<triplets, triplets, std::_Identity<triplets>, Cmp, std::allocator<triplets> >::const_iterator; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other = std::allocator<triplets>; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key> = __gnu_cxx::__alloc_traits<std::allocator<triplets>, triplets>::rebind<triplets>; typename _Alloc::value_type = triplets; std::set<_Key, _Compare, _Alloc>::value_type = triplets]’
Arrays/TwoPointer/test.cpp:29:13:   required from here
/usr/include/c  /11/bits/stl_tree.h:770:15: error: static assertion failed: comparison object must be invocable as const
  770 |               is_invocable_v<const _Compare&, const _Key&, const _Key&>,
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c  /11/bits/stl_tree.h:770:15: note: ‘std::is_invocable_v<const Cmp&, const triplets&, const triplets&>’ evaluates to false

Any idea how should I define the set?

CodePudding user response:

bool operator() (const triplets &a, const triplets &b){

should be

bool operator() (const triplets &a, const triplets &b) const{
  • Related