C ++ STL uses a red-black tree to store data inside std::setand std::map. I noticed that it set::iteratoris actually a typedef of a constant mahogany iterator:
typedef _Rb_tree<key_type, value_type, _Identity<value_type>, key_compare, _Alloc> _Rep_type;
typedef typename _Rep_type::const_iterator iterator;
This is reasonable because users should not modify the contents of the set through an iterator. But I setmust implement such operations as insertand erase, which calls a non-constant iterator of red-black tree. SGI STL uses c-style for this:
void erase(iterator __position) {
typedef typename _Rep_type::iterator _Rep_iterator;
_M_t.erase((_Rep_iterator&)__position);
}
I am wondering:
- Why is this cast safe? He throws
_Rep_type::const_iteratoron _Rep_type::iterator&. - ++? :
static_cast, const_cast . reinterpret_cast , , , C-.