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). <div><br></div><div><div>I had this code</div><div><=
br></div><div> enum Key {</div><div> Key_7=
=3D 1</div><div> Key_Current =3D 1,</div><div><br></di=
v><div> Key_8 =3D 2,</div><div> Key=
_Voltage =3D 2, </div><div><br></div><div> // ...<=
/div><div> };</div><div><br></div><div> Pair<L=
edNumer, LedNumber> getLeds(Key key) {</div><div> sw=
itch(key) {</div><div> case Key_7:</div><div>&nb=
sp; case Key_Current:</div><div> &=
nbsp; // ...</div><div> break;</di=
v><div><br></div><div> case Key_8: </div><d=
iv> case Key_Voltage:</div><div> &n=
bsp; // ...</div><div> brea=
k;</div><div> }</div><div> }</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> &nb=
sp; Pair<LedNumer, LedNumber> getLeds(Key key) {</div><div> &nb=
sp; switch(key) {</div><div> case SameVal=
ues<Key_7, Key_Current>::value:</div><div> =
// ...</div><div> break;</div><di=
v><br></div><div> case SameValues<Key_8, Key_=
Voltage>::value:</div><div> // ...</di=
v><div> break;</div><div> &n=
bsp; }</div><div> }</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 />
<br />
<br />
<br />
------=_Part_1624_6181544.1355268278356--
.