Paul Eggert <[email protected]> writes:
> On 10/18/10 04:53, Pádraig Brady wrote:
>
>> /* Like sizeof, except that it treats a variable sized array
>> as a pointer rather than determining the size at runtime. */
>> #define CT_SIZEOF(x) (__builtin_constant_p(sizeof x) ? sizeof x: sizeof
>> (void*))
>
> I don't see how this would work. If x is a variable-sized
> array, CT_SIZEOF(x) is not a constant expression, according
> to the C rules, because for A?B:C to be a constant expression,
> B and C both have to be constant expressions.
One could use __builtin_choose_expr() to avoid that particular problem:
-- Built-in Function: 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.
--
Ben Pfaff
http://benpfaff.org