C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Switch on strings

From: Breno Guimarães <brenorg_at_[hidden]>
Date: Sun, 26 Feb 2023 20:06:13 -0300
Oops: https://godbolt.org/z/r8Mvaaevf

On Sun, Feb 26, 2023 at 7:58 PM Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:

> On Sun, Feb 26, 2023 at 10:47 PM Frederick Virchanza Gotham
> <cauldwell.thomas_at_[hidden]> wrote:
> >
> > So if a 'switch' statement could work with strings, would the strings
> > need to be compile-time const arrays of char's?
>
>
> Look what I've done on GodBolt:
>
> https://godbolt.org/z/1141ov8PT
>
> And here it is copy-pasted:
>
> #include <string_view>
>
> constexpr unsigned long long MyHasher(std::string_view const arg)
> {
> unsigned long long retval = 0u;
>
> for ( auto const &c : arg ) retval += c;
>
> return retval;
> }
>
> consteval auto compile_time(auto arg)
> {
> return arg;
> }
>
> int main(int const argc, char **const argv)
> {
> switch ( MyHasher(argv[0]) )
> {
> case compile_time(MyHasher("Monkey")):
> case compile_time(MyHasher("Donkey")): ;
> }
> }
>
> The C++ core language could be changed so that the switch statement in
> 'main' could be written in shorthand as:
>
> switch<MyHasher> ( argv[0] )
> {
> case "Monkey":
> case "Donkey": ;
> }
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2023-02-26 23:06:25