C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Switch on strings

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Sun, 26 Feb 2023 13:28:28 +0000
On Sunday, February 26, 2023, Francesco Pretto wrote:
>
> is there any proposal in the works for switch on strings?
>


Maybe the switch statement could actually work with "unsigned long long",
and so then you specify a hashing algorithm that gives a digest for the
string, and all of the digests are generated at compile time. Something
like:

consteval unsigned long long MyHasher(string_view const arg)
{
    // implement hasher here
    return 0x678413a7f6780e22u;
}

void Func(string_view const arg)
{
    switch<MyHasher>( arg )
    {
        case "Monkey": ;
        case "Doggy": ;
    }
}

So the compiler generates the digest of Monkey and Doggy at compile time,
and so really the switch statement is only comparing numbers.

This new feature need not be restricted to strings, perhaps the case
statement could have any compile-time constant that can be passed as an
argument to the hasher function. So then we could use it for std::complex.

Received on 2023-02-26 13:28:31