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

如果你写了这样一个类:

class Executor {
int step1();
void step2();
int step3();
} #define FAIL -1 int main() {
// 使用Executor。调用顺序必须是1、2、3
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团队 @符风 同学。

FALSE_IT的更多相关文章

随机推荐

  1. 【Luogu】P3396哈希冲突(根号算法)

    题目链接 根号算法真的是博大精深啊……明明是暴力但复杂度就是能过 这也太强了吧!!! 预处理出p<=sqrt(n)的所有情况,耗时n根n 查询: 如果p<=根n,O1查表 如果p>= ...

  2. 【Luogu】P2801教主的魔法(分块)

    题目链接 激动qwq.这是我A的第一道分块. 分块之后对块内元素暴力sort.修改的时候对于整块打个标记,查询的时候只需要查C-tag就行了 对于非整块,暴力修改,改完之后sort 对于查询……非整块 ...

  3. hdoj--2082<母函数>

    题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=2082题目描述:26个字母各有价值,分别是1到26:给出每个字母的个数,求单词价值不超过50 的单词 ...

  4. HDU——1013Digital Roots(九余数定理)

    Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  5. iOS-跨界面传值和跨应用传值

    跨界面传值 从一个界面将一个结果值传到另一个界面,这个是我们在开发过程中非常常见的一个问题.传值本身并不是一个太复杂的问题,在此主要简述一下常用的传值方法. 我们传值常用的方法主要有四种: 1.属性传 ...

  6. form标签

    一 什么是form标签 <form> 标签用于为用户输入创建 HTML 表单. 表单用于向服务器传输数据. 二 属性 1 method method 属性规定如何发送表单数据(表单数据发送 ...

  7. LA 3905 Meteor 扫描线

    The famous Korean internet company nhn has provided an internet-based photo service which allows The ...

  8. 标准C程序设计七---45

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  9. AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128

    题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his b ...

  10. python笔记2:函数

    5. 函数 Python函数代码块以 def 关键词开头; 函数内容以冒号起始,并且缩进. *注: python中,strings, tuples, 和 numbers 是不可更改的对象,而 list ...