Topic: Allow multiple same case values if they are next to


Author: Johannes Schaub <schaub.johannes@googlemail.com>
Date: Tue, 11 Dec 2012 15:24:38 -0800 (PST)
Raw View
------=_Part_1624_6181544.1355268278356
Content-Type: text/plain; charset=ISO-8859-1

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_1624_6181544.1355268278356
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

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 e=
ach label having their own led (but the button was a single button sending =
a single key id).&nbsp;<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></di=
v><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;L=
edNumer, LedNumber&gt; getLeds(Key key) {</div><div>&nbsp; &nbsp; &nbsp; sw=
itch(key) {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; case Key_7:</div><div>&nb=
sp; &nbsp; &nbsp; &nbsp; case Key_Current:</div><div>&nbsp; &nbsp; &nbsp; &=
nbsp; &nbsp; // ...</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;</di=
v><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; case Key_8:&nbsp;</div><d=
iv>&nbsp; &nbsp; &nbsp; &nbsp; case Key_Voltage:</div><div>&nbsp; &nbsp; &n=
bsp; &nbsp; &nbsp; // ...</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; brea=
k;</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 w=
rote an ugly workaround like</div></div><div><br></div><div><div>&nbsp; &nb=
sp; Pair&lt;LedNumer, LedNumber&gt; getLeds(Key key) {</div><div>&nbsp; &nb=
sp; &nbsp; switch(key) {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; case SameVal=
ues&lt;Key_7, Key_Current&gt;::value:</div><div>&nbsp; &nbsp; &nbsp; &nbsp;=
 &nbsp; // ...</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;</div><di=
v><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; case SameValues&lt;Key_8, Key_=
Voltage&gt;::value:</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ...</di=
v><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;</div><div>&nbsp; &nbsp; &n=
bsp; }</div><div>&nbsp; &nbsp; }</div></div><div><br></div><div>Which stati=
c_asserts that all constants have the same value. But I would like to have =
native readable language support. Does anyone feel the same?</div>

<p></p>

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

------=_Part_1624_6181544.1355268278356--

.