C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Fwd: Extension to runtime polymorphism proposed

From: Jens Maurer <jens.maurer_at_[hidden]>
Date: Fri, 10 Apr 2026 07:31:46 +0200
On 4/10/26 07:10, Simon Schröder via Std-Proposals wrote:
>
>
>> On Apr 10, 2026, at 3:17 AM, Muneem via Std-Proposals <std-proposals_at_[hidden]> wrote:
>>
>> Again, why do flat maps exist, why don't we all optimize maps away if the compiler can figure out that the constant latency of a flat maps is actually gonna be faster.
>
> Regular maps exist because the performance gap between memory and CPU used to be a lot less when they were invented. And there are probably a lot of different microprocessors where a regular map is still faster than a flat map.
>
> Why don’t compilers optimize every map into a flat map? Because the compiler still does exactly what the programmer tells it to do. If the programmer decides he wants a regular map he will get one. I hope the compiler will always obey the programmer!

Please have a look at [flat.map.overview]. For example, iterator stability
guarantees are quite different for flat_map vs. a regular map.

If the compiler can prove (program-wide) that your program never relies on
those guarantees (and the other caveats), then replacing map with flat_map
is totally fine. This is beyond what compilers are currently equipped to
do, and due to the limited utility of such an optimization, I don't think
it will hit us anytime soon.

(We don't even have compiler optimization support for std::string yet,
which appears to be a much more impactful area.)

Jens

Received on 2026-04-10 05:31:51