且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

有人可以解释一下这个宏定义吗?

更新时间:2023-11-09 23:00:10

您正在查看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文档.