问题
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