C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Switch on strings

From: Antony Polukhin <antoshkka_at_[hidden]>
Date: Sun, 26 Feb 2023 15:59:10 +0300
On Sun, Feb 26, 2023, 13:56 Francesco Pretto via Std-Proposals <
std-proposals_at_[hidden]> 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++?
>

The functionality is covered by pattern matching proposal
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2688r0.pdf

An interesting fact: if you write a bunch of `if ("foo" == s)` some
compilers would convert those to a switch by string length and a comaprison
of a strings by machine words, rather than by bytes. The magic (and
performance) disappears if you put the strings to compare into an array.
The proposed pattern matching should save the magic and provide a less
exotic syntax than the utility types relying on that compiler optimization
(see llvm::StringSwitch and utils::TrivialBiMap for a syntax examples)


Regards,
> Francesco
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2023-02-26 12:59:23