|
Boost : |
From: Alexander Grund (alexander.grund_at_[hidden])
Date: 2019-11-11 08:56:02
Am 10.11.19 um 15:16 schrieb Phil Endecott via Boost:
> Regarding syntax, there isn't much wrong with just writing the
> sequence of if-else-ifs. In particular, when the next person comes
> along and needs to understand this code, which of the following
> will they prefer:
>
> if (s == "aa") f();
> else if (s == "ab") g();
> else if (s == "xy") h();
>
> or:
>
> eswitch(s) >>
> case_("aa") >> []{ f(); } >>
> case_("ab") >> []{ g(); } >>
> case_("xy") >> []{ h(); };
>
> I can see no benefit to your version, UNLESS it results in a more
> efficient implementation than the if-else-if version.
Only benefit I see is DRY: The expression to check is only mentioned
once. Think of `s` being `CalcSomeValueBasedOnInput(foo, bar, baz)`. The
proposed switch allows to put that into the switch statement and never
even introduce a variable in the source.
There is benefit in doing so, but I'd rather see this as a language
feature. Make a `switch-case-default` equivalent to `if-elseif-else` by
means of the language and have compiler warning/errors for double cases
(e.g. due to C&P) which you don't have for `if-elseif`.
So my suggestion would be to write a standards proposal and develop the
library solution around it as a PoC.
Alex
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk