Can someone please explain this macro definition?

核能气质少年 提交于 2020-11-25 02:43:01

问题


The macro definition is as follows:

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

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


回答1:


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;
}

More information can be found in the gcc documentation.



来源:https://stackoverflow.com/questions/64040245/can-someone-please-explain-this-macro-definition

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!