Aky*_*ian 4 c linux linked-list curly-braces
我在Linux中遇到了以下代码(include/linux/list.h).我对第713行很困惑.特别是,我不明白({n = pos-> member.next; 1;}).
花括号做什么?为什么这个陈述中有'1'?
如果有人能解释这一特定的行,我将不胜感激.注意,我不需要解释链接列表和#defines如何工作等.
704 /**
705 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
706 * @pos: the type * to use as a loop cursor.
707 * @n: another &struct hlist_node to use as temporary storage
708 * @head: the head for your list.
709 * @member: the name of the hlist_node within the struct.
710 */
711 #define hlist_for_each_entry_safe(pos, n, head, member) \
712 for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
713 pos && ({ n = pos->member.next; 1; }); \
714 pos = hlist_entry_safe(n, typeof(*pos), member))
715
Run Code Online (Sandbox Code Playgroud)
这是一个声明表达式.它是一个gcc扩展,根据文档6.1表达式中的语句和声明:
复合语句中的最后一件事应该是一个后跟分号的表达式; 此子表达式的值用作整个构造的值.
在这种情况下,对于代码:
({ n = pos->member.next; 1; })
Run Code Online (Sandbox Code Playgroud)
价值会是1.根据文件:
此功能在使宏定义"安全"时非常有用(因此它们只能评估每个操作数一次).
它给出了这个例子而不使用语句表达式:
#define max(a,b) ((a) > (b) ? (a) : (b))
Run Code Online (Sandbox Code Playgroud)
与这个安全版本相比,您需要知道操作数的类型:
#define maxint(a,b) \
({int _a = (a), _b = (b); _a > _b ? _a : _b; })
Run Code Online (Sandbox Code Playgroud)
这是Linux内核中使用的许多gcc扩展之一.
| 归档时间: |
|
| 查看次数: |
327 次 |
| 最近记录: |