C++ Logo


Advanced search

Re: On "transparently replaceable" in std::vector operations

From: Giuseppe D'Angelo <giuseppe.dangelo_at_[hidden]>
Date: Wed, 28 Apr 2021 21:07:16 +0200

On 28/04/2021 20:09, Edward Catmur via Std-Discussion wrote:
> Except, of course, what happens is that when the compiler detects
> one of these uses of an invalidated pointer, it silently uses the
> undefined behavior permission to subvert the program and produce
> wrong results from the point of view of the programmer. That is, it
> opens up a new class of silent and deadly errors that would not be
> there without this pointer invalidation.
> Another way to look at it is that by exhaustively testing your program
> with sanitizers enabled, you guarantee that it lacks a class of bugs and
> thus that it is safe and advantageous for the optimizer to transform
> your program accordingly.
> I do have some sympathy for your point of view... but we need the
> optimizations.

I 100% agree with the above point when it comes to iterators. I may also
agree, in principle, with pointers/references. But, let me bite the
bullet here: is there any implementation where this actually happens?

To be honest, I was more under the impression that the limitation for
pointers/references was the consequence of an "earlier" lifetime model,
more than an actual room for optimization. I find it hard to mentally
conciliate that the compiler cannot do _any_ optimization if you do the
100% equivalent work, say, in your own implementation of a `vector`
(because of the [basic.life] wording); while instead the Standard
Library a blank check for `std::vector` itself. If anything, I'd expect
a `std::invalidate(ptr)` so that I can also apply the same optimization
to my own containers...?

Thank you for the discussion,
Giuseppe D'Angelo | giuseppe.dangelo_at_[hidden] | Senior Software Engineer
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
KDAB - The Qt, C++ and OpenGL Experts

Received on 2021-04-28 14:07:23