1. Background
During the review of [P0556R1], LEWG requested
return an unspecified
value for out-of-bounds arguments. [P0556R3] was adopted in Rapperswil with
this specification. [N4791] ([bit.pow.two] 25.5.4 "Integral powers of 2"):
Returns: The minimal value
such that
y is
ispow2 ( y ) true
and; if
y >= x is not representable as a value of type
y , the result is an unspecified value.
T
At the [SAN] meeting, LEWG took a poll reconsidering this decision at the request of SG6:
Change ceil2 to hard UB, ill-formed in constexpr
SF F N A SA 11 5 1 0 0
As discussed in [P1233R0] (out-of-bounds
/
),
silently accepting out-of-bounds values can hide bugs. Making this explicitly
undefined behavior allows it to be detected and flagged by analysis tools.
2. Proposal
Wording relative to [N4791]:
template < class T > constexpr T ceil2 ( T x ) noexcept ;
- Let N be the smallest power of 2 greater than or equal to
.
x - Constraints:
Remarks: This function shall not participate in overload resolution unlessis an unsigned integer type ([basic.fundamental]).
T - Expects: N is representable as a value of type
.
T Returns: N
The minimal valuesuch that
y is
ispow2 ( y ) true
and; if
y >= x is not representable as a value of type
y , the result is an unspecified value.
T - Throws: Nothing.
- Remarks: A function call expression that violates the precondition in the Expects element is not a core constant expression ([expr.const]).
3. Discussion
For values of
that fail to satisify the Expects precondition, this
results in undefined behavior ([res.on.required] 15.5.4.11).
In earlier drafts,
and a narrow contract were intentional based on
the feedback at the [SAN] meeting. This direction was reversed during the [KonaLEWGReview].
4. History
4.1. R1 → R2
Applied feedback from [KonaLEWGReview].
-
Removed
specification, leaving decision to implementers.noexcept -
Added "Throws: Nothing"
Applied feedback from [LWGTeleconReview].
-
Wording tweaks.
4.2. R0 → R1
Applied feedback from [PostSanDiegoReview].
-
Added wording suggested by Casey Carter.
-
Added clarification that having a narrow contract and
is intentional.noexcept