[Cfp-interest 2244] Usual arithmetic conversions on unordered (unmixable) types

Vincent Lefevre vincent at vinc17.net
Mon Oct 25 01:39:07 PDT 2021


In N2601 about X.4.2 "Usual arithmetic conversions":

  If both operands have floating types and neither of the sets
  of values of their corresponding real types is a subset of
  (or equivalent to) the other, the behavior is undefined.

Instead of basic undefined behavior, shouldn't the concept of
ordered / mixable arithmetic types be introduced? The goal would
be to get a constraint violation in case of unordered / unmixable
arithmetic types (thus requiring a diagnostic), a bit like what
one gets with pointers in conditional expressions.

Then text like

  both operands have arithmetic type

in constraints would be replaced by

  both operands have ordered/mixable arithmetic type

Thus

  i ? (double) 0 : (_Decimal64) 0

would yield a constraint violation, just like

  i ? (int *) 0 : (long *) 0

FYI, on the following C code

void foo (int i)
{
  (void) (i ? (int *) 0 : (long *) 0);
  (void) (i ? (double) 0 : (_Decimal64) 0);
}

GCC gives

tst.c: In function 'foo':
tst.c:3:25: warning: pointer type mismatch in conditional expression
    3 |   (void) (i ? (int *) 0 : (long *) 0);
      |                         ^
tst.c:4:3: error: cannot mix operands of decimal floating and other floating types
    4 |   (void) (i ? (double) 0 : (_Decimal64) 0);
      |   ^

-- 
Vincent Lefèvre <vincent at vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


More information about the Cfp-interest mailing list