zoukankan      html  css  js  c++  java
  • FALSE_IT

    本文讲一个实用的语法糖(suger),很不错,攻克了我实际工作中的问题。

    如果你写了这样一个类:

    class Executor {
      int step1();
      void step2();
      int step3();
    }
    
    #define FAIL -1
    
    int main() {
      // 使用Executor。调用顺序必须是123
      Executor exec;
      if (FAIL == exec.step1()) {
           log('error');
      } else {
          exec.step2();   // 不能通过if调用,由于返回值是void
          if (FAIL == exec.step3()) {
             log('error');
          }
      }
     }

    就由于一个void返回,让代码一下子别扭了,丑不丑?
    如果能这样写该多好:

      Executor exec;
      if (FAIL == exec.step1()) {
           log('error');
      } else if (FAIL == exec.step2()) {
           log('error');
      } else if (FAIL == exec.step3()) {
           log('error');
      }

    事实上办法是有的。仅仅须要写这样一个宏:

    #define FALSE_IT(stmt) ({ (stmt); false; })
      Executor exec;
      if (SUCCESS == exec.step1()) {
           log('error');
      } else if (FALSE_IT(exec.step2()) {   // 应用FALSE_IT宏
           log('error');  // won't be here
      } else if (FAIL == exec.step3()) {
           log('error');
      }

    美丽不!

    本方法来自阿里巴巴 OceanBase团队 @符风 同学。

  • 相关阅读:
    导弹拦截
    背包求方案的字典序
    分组背包
    关于字符串的简单dp
    dp进阶——饥饿的奶牛
    压缩维度oj P1173+P1174+P1164
    搜索——迭代加深
    委外倒冲领料
    QLIKVIEW-SALESORDERDELIVERYNOTICEOUTSTOCKINVOICE
    设置采购订单供应商权限设置
  • 原文地址:https://www.cnblogs.com/slgkaifa/p/7220314.html
Copyright © 2011-2022 走看看