Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[expr.shift] p1 What is the promoted type of a source type to which integral promotion does not apply? #5398

Open
xmh0511 opened this issue Apr 19, 2022 · 3 comments

Comments

@xmh0511
Copy link
Contributor

xmh0511 commented Apr 19, 2022

[expr.shift] p1 says

The operands shall be of integral or unscoped enumeration type and integral promotions are performed. The type of the result is that of the promoted left operand.

Consider this example:

long int i = 0;
i << 1;  

The left operand's type is long int whose integer conversion rank is greater than int, thus [conv.prom] p1 cannot apply to such a type, as per the following:

A prvalue of an integer type other than bool, char8_­t, char16_­t, char32_­t, or wchar_­t whose integer conversion rank ([conv.rank]) is less than the rank of int can be converted to a prvalue of type int...

So, what is the promoted type if the integral promotion does not apply at all? Shall we improve [expr.shift] p1 to the following to clarify this vague?

The operands shall be of integral or unscoped enumeration type and integral promotions are performed(if necessary). The type of the result is that of the possibly promoted left operand.

@frederick-vs-ja
Copy link
Contributor

frederick-vs-ja commented Apr 19, 2022

There are several seemly unconditional uses of integral promotion in the current working draft. Presumably all of them mean that "performs the integral promotion and uses the result if possible, otherwise, uses the original operand or type" (promotion-or-identity).

[expr.arith.conv]/(1.5)

Otherwise, the integral promotions ([conv.prom]) shall be performed on both operands. Then the following rules shall be applied to the promoted operands:

[expr.unary.op]/7

The operand of the unary + operator shall have arithmetic, unscoped enumeration, or pointer type and the result is the value of the argument. Integral promotion is performed on integral or enumeration operands. The type of the result is the type of the promoted operand.

[expr.unary.op]/8

The operand of the unary - operator shall have arithmetic or unscoped enumeration type and the result is the negative of its operand. Integral promotion is performed on integral or enumeration operands. The negative of an unsigned quantity is computed by subtracting its value from 2n, where n is the number of bits in the promoted operand. The type of the result is the type of the promoted operand.

[expr.unary.op]/10

The operand of ~ shall have integral or unscoped enumeration type; the result is the ones’ complement of its operand. Integral promotions are performed. The type of the result is the type of the promoted operand. ...

[expr.shift]/1

... The operands shall be of integral or unscoped enumeration type and integral promotions are performed. The type of the result is that of the promoted left operand. The behavior is undefined if the right operand is negative, or greater than or equal to the width of the promoted left operand.

[over.ics.rank]/(4.2)

A conversion that promotes an enumeration whose underlying type is fixed to its underlying type is better than one that promotes to the promoted underlying type, if the two are different.

[over.built]/2

In this subclause, the term promoted integral type is used to refer to those cv-unqualified integral types which are preserved by integral promotion ([conv.prom]) (including e.g. int and long but excluding e.g. char).


I think we can disambiguate [over.ics.rank]/(4.2) and [over.built]/2 like below:

A If the underlying type of an enumeration is fixed and subject to integral promotions, a conversion that promotes an the enumeration whose underlying type is fixed to its underlying type is better than one that promotes to the promoted underlying type , if the two are different.

In this subclause, the term promoted integral type is used to refer to those cv-unqualified integral types which are preserved by not subject to integral promotion ([conv.prom]) (including e.g. int and long but excluding e.g. char).

For the rest, my favorite way is to establish a new term to express the promotion-or-identity semantics.

@jensmaurer
Copy link
Member

I agree that we should just define "promoted operand" as "operand with integral promotions applied, if any".

@xmh0511
Copy link
Contributor Author

xmh0511 commented Apr 20, 2022

Maybe, we should also clarify promoted integral type in a similar manner.

promoted integral type is used to refer to those cv-unqualified integral types that result from applying integral promotions(if any).

This could interpret why int or long is the promoted integral type but char is not since the former doesn't need to undergo promotion and the latter always be int after promotion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants