私は次のように見えるテンプレートクラスを持っています(using namespace std簡潔にするためにを仮定します):
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;
...
};
ここで、set_compareは次のように定義されます。
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);
}
};
つまり、マップはタイプTypeからstd::set<std::pair<Type, double>>値へのキーを取ります。次のような方法を使用すると、これにはいくつかの問題があります。
void insert(const Type& from, const Type& to, double weight)
{
//...
mapped_type& adj_nodes = backing_map[from];
adj_nodes.insert(make_pair(to, weight));
}
問題は、内で、setを呼び出すときにset_compare、タイプがでありconst Type&、ではないことTypeです。したがって、それがであると仮定するとstd::less、この場合、less<int>::less(const int& a, const int& b)失敗した呼び出しを試みます。
両方のcontainsがここで同じ比較関数を(効果的に)使用できるように修正する方法はありますか?