C++ Logo

std-proposals

Advanced search

Re: [std-proposals] A draft for modern switch

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Tue, 27 May 2025 09:41:45 -0400
On Tue, May 27, 2025 at 9:32 AM Fabio Alemagna via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> Il giorno mar 27 mag 2025 alle ore 15:23 Thiago Macieira via Std-Proposals <std-proposals_at_[hidden]> ha scritto:
>>
>> [...] An equality operator
>> alone won't suffice, because then the order of case labels would matter, which
>> seems ill-advised to me.
>
>
> Why would the order matter? You're looking for an item in a unordered set. Of course, having other operators or even a hash function defined for the specific type would make it possible to optimize the search.

Well, the OP said, "because we can require switch (string) to perform
O(log(N)) comparisons, where N is the number of cases." That sounds
like some kind of ordered search.

And if such optimizations are so important, it seems to me that
pattern matching is a much more reasonable place to add such a
requirement and such functionality. It already works with user-defined
types, so it'd be easier to add that requirement. If all of the
conditions are constant expression values, and the type is ordered
(and all of those constant expressions are within the ordering of the
type), then the implementation is required to only do O(log(N))
comparisons.

> As I've mentioned earlier, there's a library implementation of this. It can of course be made better, but the core functionality is there. Have a look: https://github.com/falemagn/uberswitch
>
> Fabio

Received on 2025-05-27 13:41:59