C++ Logo


Advanced search

Re: [std-proposals] Switch on strings

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Sun, 26 Feb 2023 08:39:31 -0500
On Sun, Feb 26, 2023 at 5:56 AM Francesco Pretto via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> is there any proposal in the works for switch on strings? With all the
> constexpr constructors and equality operators in place, what's missing
> to finally have such construct in modern C++?

(1) Practical objection: `char_traits`. Even if it's "easy" to switch on
`std::basic_string<char, std::char_traits<char>, std::allocator<char>>`,
you'll need to figure out how to switch on `std::basic_string<wchar_t,
MyCustomTraitsClass, MyCustomAllocator<char>>`. The allocator can safely
be ignored — let's just pretend that this hypothetical "operator switch" is
really working on an argument of type `std::string_view`, not `std::string`
— but you still need to figure out what to do about char_traits.

(2) Political objection: `switch` sucks. It's a relic of C, like `goto`.
Its semantics are confusing (fallthrough! Duff's Device!). It is quick to
teach both because it is rarely used and because it is relatively simple.
You seem to be proposing to make it (still rarely used but) relatively more
complicated, thus forcing students to spend longer learning it... but why
should we *want* people to spend more time on `switch`? That time could be
better spent elsewhere in the language.


Received on 2023-02-26 13:39:45