I have a templated class which looks as follows (assume a using namespace std for brevity):
template <typename Type, typename Compare = less<Type>>
struct weighted_base
{
typedef typename set<pair<Type, double>, set_compare<Type, Compare>> mapped_type;
map<Type, mapped_type, Compare> backing_map;
...
};
where set_compare is defined as:
template <typename Type, typename Compare>
struct set_compare
{
bool operator()(const pair<Type, double>& a,
const pair<Type, double>& b)
{
return Compare(a.first, b.first);
}
};
That is, the map takes keys of type Type to std::set<std::pair<Type, double>> values. This has some problems when I use a method such as:
void insert(const Type& from, const Type& to, double weight)
{
//...
mapped_type& adj_nodes = backing_map[from];
adj_nodes.insert(make_pair(to, weight));
}
The problem is that within the set, when it goes to call set_compare, it has a type of const Type&, not Type. Thus, assuming it’s std::less, in this case it will be trying to call less<int>::less(const int& a, const int& b) which fails.
Is there some way of fixing it so that both contains can use (effectively) the same comparison function here?
Compareis the type of a binary functor, so you probably needwhere
Compare()is a temporary, defualt constructed,Compareinstance. For example, if you substituteCompareforstd::less<int>,Your error is likely to give you many compiler errors, some of which could send you down the wrong track. I suggest fixing it first, then seeing if the code behaves as you expect it to.