News for hackers. Stuff that matters.
Re:Will It Optimize? by merraksh on Friday September 16, 2011 @10:48AM (#3004616)
I have a little doubt about number 6. A switch() without a "default:" label has undefined behavior if x is negative or above 5, while nothing would happen in the cascading if's. Wouldn't a correct optimization of that be

  void function(int x) {
    switch (x) {
      case 0: f0(); break;
      case 1: f1(); break;
      case 2: f2(); break;
      case 3: f3(); break;
      case 4: f4(); break;
      case 5: f5(); break;
      default:
    }
  }
Re:Will It Optimize? by dchest on Friday September 16, 2011 @11:03AM (#3004706)
Why does it have undefined behavior? Switch statement with no match and without the default case has defined behavior: the switch statement does nothing.
Re:Will It Optimize? by rcfox on Friday September 16, 2011 @12:15PM (#3005054)
"If no converted case constant expression matches and there is no default label, no part of the switch body is executed."

Section 6.8.4.2, item 5 of http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf (page 134)

Re:Will It Optimize? by cbr on Friday September 16, 2011 @04:11PM (#3006115)
how is this different from "does nothing"?
Re:Will It Optimize? by rcfox on Friday September 16, 2011 @04:55PM (#3006264)
... it's not? I was just providing an authoritative reference for the parent's claim.

(Pedantic note: It's technically incorrect to say that the switch statement does nothing.

    switch(FunctionThatDoesSomethingAndReturns1()) {
        case 2:
            // foo
    }
The switch body does nothing, but the actual statement does something.)
Re:Will It Optimize? by thefre on Friday September 16, 2011 @12:11PM (#3005032)
are you high?