Every one of us has (probably) had the childhood dream of writing:
switch(my_std_string) {
case "foo": do_stuff(); break;
case "bar": do_other_stuff(); break;
default: just_give_up();
}
but this is not possible, as is explained in the answers to this question from the olden days (2009):
Why the switch statement cannot be applied on strings?
Since then we've seen the advent of C++11, which lets us go as far as:
switch (my_hash::hash(my_std_string)) {
case "foo"_hash: do_stuff(); break;
case "bar"_hash: do_other_stuff(); break;
default: just_give_up();
}
as described in an answer to Compile time string hashing - which is not so bad, although it doesn't actually do exactly what we wanted - there's a chance of collision.
My question is: Has the development of the language since then (mostly C++14 I suppose) affected the way one would write a sort-of-a string case statement? Or simplified the nuts-and-bolts for achieving the above?
Specifically, with the conclusion of the C++17 standard being just around the corner - I'm interested in the answer given what we can assume the standard will contain.
It would be easy-ish to write
to build a statically sized hash table of
case0
throughcaseN
, populate it dynamically, test for collisions with==
, do the lookup viaexpr
, and run the corresponding lambda.Even
caser(case3)->*caser(case4)->*lambda
and->*fallthrough
could be supported.I do not see a compelling need.
I see no advantage to writing this in C++17 either.