C++ Logo

STD-PROPOSALS

Advanced search

Subject: Re: [std-proposals] std::iterator_static_cast & std::iterator_const_cast?
From: Arthur O'Dwyer (arthur.j.odwyer_at_[hidden])
Date: 2019-05-04 10:38:39


Notice that all standard containers already provide a ranged erase member
function which can be used to "launder" a const_iterator into an iterator
in a type-safe way.
(And of course you don't need to "launder" an iterator into a
const_iterator; that direction is already implicitly convertible.)

C ctr;
C::const_iterator cit = ctr.begin(); // iterator to const_iterator happens
automatically
C::iterator it = ctr.erase(cit, cit); // const_iterator to iterator is
always doable, but requires non-const access to the container itself

If you have a const_iterator and you *don't* have non-const access to the
container itself, then you really *shouldn't be trying* to get a non-const
iterator. That would break type-safety.

P.S., just throwing this out there, not a serious suggestion — if you make
`iterator` a private base class of `const_iterator`, then you can use a
C-style cast (and *only* a C-style cast) to perform the unsafe conversion
from const_iterator to iterator. A *private* inheritance relationship is
usually indistinguishable from *no* inheritance relationship, but unsafe
casts are one place where even a private relationship can come to light.

HTH,
–Arthur



STD-PROPOSALS list run by herb.sutter at gmail.com

Standard Proposals Archives on Google Groups