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
be the smallest power of 2 greater than or equal to
N .
x - Expects:
is representable as a value of
N .
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 - Constraints:
Remarks: This function shall not participate in overload resolution unlessis an unsigned integer type ([basic.fundamental]).
T - Remarks: Call expressions that violate the precondition in the Expects element are not core constant expressions ([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).
This function is intentionally
with a narrow contract:
-
is a low-level function.ceil2 -
Reflector recollection of the San Diego meeting indicates this was the desire of LEWG, although minutes for this discussion are limited.
4. History
4.1. R0 → R1
Applied feedback from [PostSanDiegoReview].
-
Added wording suggested by Casey Carter.
-
Added clarification that having a narrow contract and
is intentional.noexcept