On Apr 8, 4:46 pm, Pete Becker <p...@versatilecoding.com> wrote:
> Victor Bazarov wrote:
> > keith wrote:
> >> Is the following code well-formed and portable?
> >> Value value;
> >> Container<Value> c;
> >> c.push_back(value);
> >> c.erase(--c.end());
> >> Container can be std::string, std::vector...
> > First thing that came to my mind: c.end() returns a
> > *temporary* which isn't an lvalue. Decrement requires an
> > lvalue, hence the code that has (--c.end()) is ill-formed.
> > Ill-formed program cannot be portable. So, the answer to
> > your question is "No".
> It's not quite that simple. If the iterator is a class type you can
> decrement it.
Maybe. If the iterator is a class type and the increment
operator is a member, you can decrement it. If the increment
operator is a free function, taking the iterator as a non-const
reference, you can't. (I don't know of any implementations
which do it that way, but I don't know why. It's the way I'd do
it.)
--
James Kanze
|