有人可以解释一下这个宏定义吗? [英] Can someone please explain this macro definition?

查看:106
本文介绍了有人可以解释一下这个宏定义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

宏定义如下:

#define open_listen_fd_or_die(port) \
   ({ int rc = open_listen_fd(port); assert(rc >= 0); rc; })

open_listen_fd()是一个函数,它返回整数值.
我的问题:第三个陈述rc;的意义是什么?

open_listen_fd() is a function, that returns integer value.
My question: What is the significance of third statement rc; ?

推荐答案

您正在查看gcc扩展名,该扩展名允许您将多个语句视为单个表达式.最后一个需要是一个表达式,该表达式被用作整个事物的结果.它旨在用于宏中以允许存在临时变量,但是在现代C语言中,在这种情况下以及其他许多情况下,最好使用内联函数而不是像宏之类的函数(不仅仅是因为这是非标准扩展和内联函数)是标准的):

You're looking at a gcc extension that allows you to treat multiple statements as a single expression. The last one needs to be an expression that is used as the result of the entire thing. It's meant for use in macros to allow the presence of temporary variables, but in modern C it's better to use inline functions instead of function like macros in this and many other cases (and not just because this is a non-standard extension and inline functions are standard):

inline int open_listen_fd_or_die(int port) {
    int rc = open_listen_fd(port);
    assert(rc >= 0); // maybe not the best approach
    return rc;
}

更多信息,请参见 gcc文档.

这篇关于有人可以解释一下这个宏定义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆