有人可以解释一下这个宏定义吗?
Can someone please explain this macro definition?
宏定义如下:
#define open_listen_fd_or_die(port) \
({ int rc = open_listen_fd(port); assert(rc >= 0); rc; })
open_listen_fd()
是一个函数,即returns整数值。
我的问题:第三个陈述 rc;
的意义是什么?
您正在查看一个 gcc 扩展,它允许您将多个语句视为一个表达式。最后一个需要是一个表达式,用作整个事物的结果。它意味着在宏中使用以允许存在临时变量,但在现代 C 中,在这种情况和许多其他情况下,最好使用内联函数而不是类似宏的函数(不仅仅是因为这是 non-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 documentation 中找到更多信息。
宏定义如下:
#define open_listen_fd_or_die(port) \
({ int rc = open_listen_fd(port); assert(rc >= 0); rc; })
open_listen_fd()
是一个函数,即returns整数值。
我的问题:第三个陈述 rc;
的意义是什么?
您正在查看一个 gcc 扩展,它允许您将多个语句视为一个表达式。最后一个需要是一个表达式,用作整个事物的结果。它意味着在宏中使用以允许存在临时变量,但在现代 C 中,在这种情况和许多其他情况下,最好使用内联函数而不是类似宏的函数(不仅仅是因为这是 non-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 documentation 中找到更多信息。