本文讲一个实用的语法糖(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. HDU——1027Ignatius and the Princess II(next_permutation函数)

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  2. 给某个li标签家样式

    HTML: <div class="tabs clearfix"> <ul id="der"> <li ><a hre ...

  3. Docker部署注册中心、Docker创建私有镜像库、自签名证书、Deploy a registry server

    这是我在内部部署Docker Registry时记录下来的笔记,操作环境是Centos 7.Docker 18.06.1-ce 1.运行registry 我当前所使用的主机的IP是192.168.1. ...

  4. 如何用DW设计界面 结合 VS设计后台代码

    原文发布时间为:2008-11-02 -- 来源于本人的百度文章 [由搬家工具导入] 问:在vs.net里有form标记,而dw里却没有,两个里面的标记代码都不一样,怎么能通用? 在.net里修改dw ...

  5. JS函数调用原理($.ajax原理)

    参考四种函数调用:http://www.cnblogs.com/qlqwjy/p/7660013.html http://www.cnblogs.com/qlqwjy/p/7660375.html 1 ...

  6. CSS-滤镜 -webkit-filter

    css滤镜属性,可以在元素呈现之前,为元素的渲染提供一些效果,如模糊.颜色转移之类的.滤镜常用于调整图像.背景.边框的渲染. 语法: webkit-filter: none | blur(px) | ...

  7. 2016-2017 ACM-ICPC Southeastern European Regional Programming Contest (SEERC 2016)

    题目链接  Codefores_Gym_101164 Solved  6/11 Penalty Problem A Problem B Problem C Problem D Problem E Pr ...

  8. F#周报2019年第21期

    新闻 F#在GitHub上的开发仓库现在变为dotnet/fsharp Ionide 4.0路线图 Fable的五月公告 Visual Studio 2019版本16.1 WinUI 3.0路线图 欢 ...

  9. BZOJ——2438: [中山市选2011]杀人游戏

    http://www.lydsy.com/JudgeOnline/problem.php?id=2438 Description 一位冷血的杀手潜入 Na-wiat,并假装成平民.警察希望能在 N 个 ...

  10. QBXT T15565 Day4上午道路分组

    有向并查集维护连通性 优化: vis数组表示能被节点1到达的点 显然,已经分在一个联通块中的点就没必要在用该点扩展了. #include<cstdio> #include<algor ...