Why would you use the ternary operator without assigning a value for the “true” condition (x = x ?: 1)

前端 未结 7 1656
旧时难觅i
旧时难觅i 2020-12-03 09:38

In the Android open-source qemu code I ran across this line of code:

machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */

7条回答
  •  一生所求
    2020-12-03 10:07

    It's a GCC extension, and it gets more interesting and useful when the condition has side effects.

    In this case, yes, I for one would agree it's obscure more than anything else.

提交回复
热议问题