| __builtin_choose_expr

Previous | Other built-in functions provided by GCC | Next

`type __builtin_choose_expr (const_exp, exp1, exp2);`
You can use the built-in function __builtin_choose_expr to evaluate code depending on the value of a constant expression. This built-in function returns exp1 if const_exp, which is a constant expression that must be able to be determined at compile time, is nonzero. Otherwise it returns 0.
This built-in function is analogous to the `? :` operator in C, except that the expression returned has its type unaltered by promotion rules. Also, the built-in function does not evaluate the expression that was not chosen. For example, if const_exp evaluates to true, exp2 is not evaluated even if it has side-effects.
This built-in function can return an lvalue if the chosen argument is an lvalue.
If exp1 is returned, the return type is the same as exp1's type. Similarly, if exp2 is returned, its return type is the same as exp2.
Example:

    #define foo(x)                                                    \
      __builtin_choose_expr (                                         \
        __builtin_types_compatible_p (typeof (x), double),            \
        foo_double (x),                                               \
        __builtin_choose_expr (                                       \
          __builtin_types_compatible_p (typeof (x), float),           \
          foo_float (x),                                              \
          /* The void expression results in a compile-time error  \
             when assigning the result to something.  */          \
          (void)0))

Note: The unused expression (exp1 or exp2 depending on the value of const_exp) may still generate syntax errors. This may change in future revisions.