Bizarre use of conditional operator in Linux

后端 未结 4 1801
长发绾君心
长发绾君心 2020-12-05 23:23

In the 3.0.4 Linux kernel, mm/filemap.c has this line of code:

retval = retval ?: desc.error;

I\'ve tried compiling a similar minimal test

4条回答
  •  盖世英雄少女心
    2020-12-05 23:30

    This is a GCC extension called Conditionals with Omitted Operands. Omitting the middle operand has the effect of using the value of the conditional as the omitted operand without evaluating it again. It is safe to use even if the conditional is a macro.

提交回复
热议问题