Topic: Allow multiple same case values if they are next


Author: Arthur Tchaikovsky <atch.cpp@gmail.com>
Date: Wed, 12 Dec 2012 03:27:41 -0800 (PST)
Raw View
------=_Part_123_24783969.1355311661657
Content-Type: text/plain; charset=ISO-8859-1

I agree with you, and think that this should be amended. After all we have
'case' not 'value' in switch statement. So different cases should be
allowed independently of their values.

On Tuesday, 11 December 2012 23:24:38 UTC, Johannes Schaub wrote:

> Here is a simple proposal for allowing multiple consecutive identical case
> values. My use-case were buttons that have a left and a right label, with
> each label having their own led (but the button was a single button sending
> a single key id).
>
>  I had this code
>
>     enum Key {
>       Key_7 = 1
>       Key_Current = 1,
>
>       Key_8 = 2,
>       Key_Voltage = 2,
>
>       // ...
>     };
>
>     Pair<LedNumer, LedNumber> getLeds(Key key) {
>       switch(key) {
>         case Key_7:
>         case Key_Current:
>           // ...
>           break;
>
>         case Key_8:
>         case Key_Voltage:
>           // ...
>           break;
>       }
>     }
>
> C++ does't like this. It would be nice if this was allowed. I wrote an
> ugly workaround like
>
>      Pair<LedNumer, LedNumber> getLeds(Key key) {
>       switch(key) {
>         case SameValues<Key_7, Key_Current>::value:
>           // ...
>           break;
>
>         case SameValues<Key_8, Key_Voltage>::value:
>           // ...
>           break;
>       }
>     }
>
> Which static_asserts that all constants have the same value. But I would
> like to have native readable language support. Does anyone feel the same?
>

--




------=_Part_123_24783969.1355311661657
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<DIV>I agree with you, and think that this should be amended. After all we =
have 'case' not 'value' in switch statement. So different cases should be a=
llowed independently of their values.</DIV>
<DIV><BR>On Tuesday, 11 December 2012 23:24:38 UTC, Johannes Schaub wrote:<=
/DIV>
<BLOCKQUOTE style=3D"BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex=
; PADDING-LEFT: 1ex" class=3Dgmail_quote>Here is a simple proposal for allo=
wing multiple consecutive identical case values. My use-case were buttons t=
hat have a left and a right label, with each label having their own led (bu=
t the button was a single button sending a single key id).&nbsp;=20
<DIV><BR></DIV>
<DIV>
<DIV>I had this code</DIV>
<DIV><BR></DIV>
<DIV>&nbsp; &nbsp; enum Key {</DIV>
<DIV>&nbsp; &nbsp; &nbsp; Key_7 =3D 1</DIV>
<DIV>&nbsp; &nbsp; &nbsp; Key_Current =3D 1,</DIV>
<DIV><BR></DIV>
<DIV>&nbsp; &nbsp; &nbsp; Key_8 =3D 2,</DIV>
<DIV>&nbsp; &nbsp; &nbsp; Key_Voltage =3D 2,&nbsp;</DIV>
<DIV><BR></DIV>
<DIV>&nbsp; &nbsp; &nbsp; // ...</DIV>
<DIV>&nbsp; &nbsp; };</DIV>
<DIV><BR></DIV>
<DIV>&nbsp; &nbsp; Pair&lt;LedNumer, LedNumber&gt; getLeds(Key key) {</DIV>
<DIV>&nbsp; &nbsp; &nbsp; switch(key) {</DIV>
<DIV>&nbsp; &nbsp; &nbsp; &nbsp; case Key_7:</DIV>
<DIV>&nbsp; &nbsp; &nbsp; &nbsp; case Key_Current:</DIV>
<DIV>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ...</DIV>
<DIV>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;</DIV>
<DIV><BR></DIV>
<DIV>&nbsp; &nbsp; &nbsp; &nbsp; case Key_8:&nbsp;</DIV>
<DIV>&nbsp; &nbsp; &nbsp; &nbsp; case Key_Voltage:</DIV>
<DIV>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ...</DIV>
<DIV>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;</DIV>
<DIV>&nbsp; &nbsp; &nbsp; }</DIV>
<DIV>&nbsp; &nbsp; }</DIV>
<DIV><BR></DIV>
<DIV>C++ does't like this. It would be nice if this was allowed. I wrote an=
 ugly workaround like</DIV></DIV>
<DIV><BR></DIV>
<DIV>
<DIV>&nbsp; &nbsp; Pair&lt;LedNumer, LedNumber&gt; getLeds(Key key) {</DIV>
<DIV>&nbsp; &nbsp; &nbsp; switch(key) {</DIV>
<DIV>&nbsp; &nbsp; &nbsp; &nbsp; case SameValues&lt;Key_7, Key_Current&gt;:=
:value:</DIV>
<DIV>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ...</DIV>
<DIV>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;</DIV>
<DIV><BR></DIV>
<DIV>&nbsp; &nbsp; &nbsp; &nbsp; case SameValues&lt;Key_8, Key_Voltage&gt;:=
:value:</DIV>
<DIV>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ...</DIV>
<DIV>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;</DIV>
<DIV>&nbsp; &nbsp; &nbsp; }</DIV>
<DIV>&nbsp; &nbsp; }</DIV></DIV>
<DIV><BR></DIV>
<DIV>Which static_asserts that all constants have the same value. But I wou=
ld like to have native readable language support. Does anyone feel the same=
?</DIV></BLOCKQUOTE>

<p></p>

-- <br />
&nbsp;<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_123_24783969.1355311661657--

.