This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: PATCH for division by zero in template arguments
>>>>> "Jason" == Jason Merrill <jason@cygnus.com> writes:
>>>>> Mark Mitchell <mark@markmitchell.com> writes:
>> OK. Still, since programs that divide by a constant 0 are
>> ill-formed, not merely undefined, I think this should be a
>> pedwarn.
Jason> Why do you say that?
Teach me not to quote chapter and verse when I'm claiming something
about the standard. Here's why:
[expr]
If during the evaluation of an expression, the result is not mathemat-
ically defined or not in the range of representable values for its
type, the behavior is undefined, unless such an expression is a con-
stant expression (_expr.const_), in which case the program is ill-
formed.
Note that this also implies, for example, that integer overflow in a
constant expression *must* be diagnosed. A strict reading of this
might seem to imply, in the division by zero case, that both operands
must be constant, but that doesn't really make any sense.
>> I also think the result of the operation should not be marked
>> TREE_CONSTANT, since it's not: it's a run-time division.
Jason> I'd rather handle this by checking that the expression
Jason> actually reduces to an INTEGER_CST, as in
Jason> check_cp_case_value or build_enumerator.
I considered and rejected this solution. Why call something that must
be evaluated at run-time a constant? For example, I checked that code
like this:
extern const int i = 7 % 0;
yields a run-time initialization. This means that we're treating this
as a dynamic initialization, and that it will be done after static
initializations. And this means that the expression isn't really
constant in the sense that we usually mean constant. I think it's
very non-intuitive to call such things constant. In fact, we don't
even have any way (in the front-end) of knowing that `7 % 0' will
evaluate to the same thing twice.
Jason> Jason
--
Mark Mitchell mark@markmitchell.com
Mark Mitchell Consulting http://www.markmitchell.com