C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Switch on strings

From: Lénárd Szolnoki <cpp_at_[hidden]>
Date: Sun, 26 Feb 2023 12:39:14 +0000
On 26 February 2023 12:16:22 GMT, Bo Persson via Std-Proposals <std-proposals_at_[hidden]> 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?

There are many possible optimisations for a hypothetical switch-case with many string cases. The most simple is probably binary search in lexicographical order. The state of the art I believe is "perfect hashing".

A library solution might be possible. In any case it might be useful in the standard library or in the language,

Cheers,
Lénárd

Received on 2023-02-26 12:39:19