C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Switch on strings

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Tue, 28 Feb 2023 11:34:39 +0100
pon., 27 lut 2023 o 18:08 Ray Gardener via Std-Proposals
<std-proposals_at_[hidden]> napisał(a):
>
> How about a std::map<T, std::function(...)> and a function that finds a match to a given key and executes the value for the key?
>
> Ray Gardener

Using map as a switch is a bad idea because you often pay a lot more
for functionalities that are irrelevant for usage you need.
Better have a dedicated construct that is predefined on compiler time
and this allows better optimization opportunities.
In one YouTube video form "Weekly With Jason Turner" he create dumb
constexpr map that can be very easy optimalziet out by compiler:
see: https://www.youtube.com/watch?v=INn3xa4pMfg


>
>
> On Sun, Feb 26, 2023 at 11:39 PM Sebastian Wittmeier via Std-Proposals <std-proposals_at_[hidden]> wrote:
>>
>> Or
>>
>>
>>
>> (3) Depending on the possible values of the string variable use a fast hashing that has no collisions.
>>
>>
>>
>>
>> For strings you probably want to compare not only for equalness, but at least
>>
>> * case insensitive
>>
>> * ignoring white sace
>>
>> * starts with prefix
>>
>> * Unicode aware (different Unicode variants that lead to the same result)
>>
>> * or even full blown regular expressions.
>>
>>
>>
>> Should all those be also included into switch?
>>
>>
>>
>> Then the advanced switch is more like a replacement for chained if else cascades.
>>
>>
>>
>> Best,
>>
>> Sebastian
>>
>>
>>
>> -----Ursprüngliche Nachricht-----
>> Von: Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]>
>> Gesendet: Mo 27.02.2023 01:04
>> Betreff: Re: [std-proposals] Switch on strings
>> An: std-proposals_at_[hidden];
>> CC: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>;
>>
>> On Sun, Feb 26, 2023 at 11:06 PM Breno Guimarães <brenorg_at_[hidden]> wrote:
>> >
>> > Oops: https://godbolt.org/z/r8Mvaaevf
>>
>>
>> The solution to collisions is one of the following:
>>
>> (1) Use a better hash algorithm that won't collide
>> (2) After the hash matches, use the equality operator to be sure
>>
>> But if you're going to use the equality operator at all then you could
>> get rid of the hashing altogether -- unless you want to keep it in
>> there for speed.
>> --
>> Std-Proposals mailing list
>> Std-Proposals_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>
>> --
>> Std-Proposals mailing list
>> Std-Proposals_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2023-02-28 10:34:50