Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why can switch only compare to const values?

8 views
Skip to first unread message

Kevin Peterson

unread,
Mar 22, 2013, 10:34:00 AM3/22/13
to help-gp...@gnu.org
Just curious:

Take a switch-statement like -

switch(myVar)
{
case(label):
...
break;
}

why label should be constant only?

Nikos Chantziaras

unread,
Mar 22, 2013, 1:54:38 PM3/22/13
to help-gp...@gnu.org
Because it can be ambiguous otherwise:

int a = 1, b = 1;
int val = 1;

switch (val) {
case a: foo(); break;
case b: bar(); break;
}

Requiring constant expressions for the cases makes sure that there's no
ambiguity, since they can be evaluated at compile time.



0 new messages