help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why can switch only compare to const values?


From: Nikos Chantziaras
Subject: Re: Why can switch only compare to const values?
Date: Fri, 22 Mar 2013 19:54:38 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130307 Thunderbird/17.0.4

On 22/03/13 16:34, Kevin Peterson wrote:
Just curious:

Take a switch-statement like -

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

why label should be constant only?

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.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]