0

I was looking at the source for weston, the wayland reference implementation, and I saw this line that confused me (https://github.com/wayland-project/weston/blob/master/desktop-shell/shell.c line 230)

return snprintf(buf, len, "%s window%s%s%s%s%s",
    "top-level",
    t ? " '" : "", t ?: "", t ? "'" : "",
    c ? " of " : "", c ?: "");

What does t ?: "" mean? I've never seen an operator like this before, in C at least. It looks like it means "if t is not null then t else (elseclause)" but I didn't know C had that operator.

user973223
  • 127
  • 1
  • 8

1 Answers1

3

This is a GCC extension to C. x ? : y acts like x ? x : y except that x is evaluated only once. So, if x is an expression with side effects, such as n++, the side effects occur only once. This is useful in macros where the condition may be or may include arguments to the macro that are expressions that we want to evaluate only once.

In the code shown in the question, it is not used for this benefit; it is used only to shorten the code slightly.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312