通过解析器的代码时,例如clang编译器的clang/Parse目录中的Parser.cpp

switch (Close) {
default: break;
case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
case tok::greater:  LHSName = "<"; DID = diag::err_expected_greater; break;
case tok::greatergreatergreater:
                    LHSName = "<<<"; DID = diag::err_expected_ggg; break;
}

我看到default在开头。是否有任何理由保持这种方式。通常我们将default保留在最后,所以我有点困惑。

最佳答案

我唯一一次看到默认问题的位置是在执行这样的操作时,它是Duff's Device类型的构造,并且可能永远不应在现代平台上尝试。 :)


void copyAligned4Bytes(u32 *in, u32 *out, int numBytes)
{
  assert((numBytes & 0x03) == 0);

  while(numBytes)
  {
    switch(numBytes)
    {
      default: *out++ = *in++; numBytes -= 4;
      case 12: *out++ = *in++; numBytes -= 4;
      case 8: *out++ = *in++; numBytes -= 4;
      case 4: *out++ = *in++; numBytes -= 4;
    }
  }
}

嗯,用C编码的乐趣越接近汇编器,以获得某种类型的跳转表。

10-06 02:07