Search code examples
c++boostbimap

boost bimap set_of with user defined comparator


I plan to use my own compare function with boost bimap. The issue i am trying to address is when i use boost bimap with a pointer, the comparison should not compare the two pointers but should compare the class which is pointed by the pointer.

I tried the following code. But it doesn't even compile. What am i doing wrong? Also is there a simpler way to achieve less function that compares two objects and not two pointers pointers)

typedef std::set<int> ruleset;

template <class myclass>
bool comp_pointer(const myclass &lhs, const myclass &rhs)
{
    return ((*lhs) < (*rhs));
}

typedef boost::bimap<set_of<ruleset *, comp_pointer<ruleset *> >, int> megarulebimap;

Error messages:

party1.cpp:104:64: error: type/value mismatch at argument 2 in template parameter list for 'template struct boost::bimaps::set_of' party1.cpp:104:64: error: expected a type, got 'comp_pointer' party1.cpp:104:70: error: template argument 1 is invalid party1.cpp:104:85: error: invalid type in declaration before ';' token


Solution

  • typedef std::set<int> ruleset;
    
    struct ruleset_cmp {
        bool operator()(const ruleset *lhs, const ruleset *rhs) const
        {
            return ((*lhs) < (*rhs));
        }
    };
    
    typedef boost::bimap<set_of<ruleset *, ruleset_cmp>, int> megarulebimap;
    

    Okay. The above snippet works. It appears a functor needs to be used here.