Post by David WilkinsonCode compiles on VC10 if you make A::m_n mutable and A::Change() const, but this
will break the set. This trick could be used to make inconsequential changes
(ones that did not change the ordering).
I always think mutable is a bit of a hack as const setter functions seem
somehow dodgy to me, instead I have just written the following:
template <typename T, typename Pr = std::less<typename T::key_type const>,
typename Alloc = std::allocator<std::pair<typename T::key_type const, T> > >
class mutable_set : public std::map<typename T::key_type, T, Pr, Alloc>
{
typedef typename T::key_type key_type;
typedef std::map<key_type, T, Pr, Alloc> base_type;
public:
class iterator : public base_type::iterator
{
public:
iterator() {}
iterator(typename base_type::iterator aIterator) :
base_type::iterator(aIterator) {}
T* operator->() const { return
&base_type::iterator::operator*().second; }
T& operator*() const { return base_type::iterator::operator*().second; }
};
class const_iterator : public base_type::const_iterator
{
public:
const_iterator() {}
const_iterator(typename base_type::const_iterator aIterator) :
base_type::const_iterator(aIterator) {}
const T* operator->() const { return
&base_type::const_iterator::operator*().second; }
const T& operator*() const { return
base_type::const_iterator::operator*().second; }
};
public:
iterator insert(const T& aValue)
{
return
iterator(base_type::insert(std::make_pair(static_cast<key_type>(aValue),
aValue)).first);
}
};
struct foo
{
struct key_type
{
bool operator<(const key_type& other) const { return true; }
};
operator key_type() const { return key_type(); }
};
int main()
{
mutable_set<foo> s;
s.insert(foo());
}