C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Switch on strings

From: Andrey Semashev <andrey.semashev_at_[hidden]>
Date: Sun, 26 Feb 2023 15:25:17 +0300
On 2/26/23 15:16, Bo Persson via Std-Proposals wrote:
> On 2023-02-26 at 11:56, Francesco Pretto via Std-Proposals wrote:
>> Hello,
>>
>> is there any proposal in the works for switch on strings? With all the
>> constexpr constructors and equality operators in place, what's missing
>> to finally have such construct in modern C++?
>>
>
> With integers, if they have consecutive values, you can optimize the
> switch by using an array of jump-targets and index it with the case-values.
>
> Strings cannot be used as an array index, so it would still be the same
> as a set of if-else statements. So no performance gain. Then why bother?

The compiler could generate a binary lookup tree or prefix analysis
automatically based on the string contents.

Received on 2023-02-26 12:25:32