Created on 2017-03-24.00:00:00 last changed 44 months ago
Proposed resolution:
This wording is relative to N4659.
Edit [support.types.byteops] as indicated:
template <class IntType> constexpr byte& operator<<=(byte& b, IntType shift) noexcept;-1- Remarks: This function shall not participate in overload resolution unless is_integral_v<IntType> is true.
-2- Effects: Equivalent to: return b = b << shiftbyte(static_cast<unsigned char>(b) << shift);template <class IntType> constexpr byte operator<<(byte b, IntType shift) noexcept;-3- Remarks: This function shall not participate in overload resolution unless is_integral_v<IntType> is true.
-4- Effects: Equivalent to: return static_cast<byte>(static_cast<unsigned char>(static_cast<unsigned int>(b) << shift))byte(static_cast<unsigned char>(b) << shift);template <class IntType> constexpr byte& operator>>=(byte& b, IntType shift) noexcept;-5- Remarks: This function shall not participate in overload resolution unless is_integral_v<IntType> is true.
-6- Effects: Equivalent to: return b = b >> shiftbyte(static_cast<unsigned char>(b) >> shift);template <class IntType> constexpr byte operator>>(byte b, IntType shift) noexcept;-7- Remarks: This function shall not participate in overload resolution unless is_integral_v<IntType> is true.
-8- Effects: Equivalent to: return static_cast<byte>(static_cast<unsigned char>(static_cast<unsigned int>(b) >> shift))byte(static_cast<unsigned char>(b) >> shift);constexpr byte& operator|=(byte& l, byte r) noexcept;-9- Effects: Equivalent to:
return l = l | rbyte(static_cast<unsigned char>(l) | static_cast<unsigned char>(r));constexpr byte operator|(byte l, byte r) noexcept;-10- Effects: Equivalent to:
return static_cast<byte>(static_cast<unsigned char>(static_cast<unsigned int>(l) | static_cast<unsigned int>(r)))byte(static_cast<unsigned char>(l) | static_cast<unsigned char>(r));constexpr byte& operator&=(byte& l, byte r) noexcept;-11- Effects: Equivalent to:
return l = l & rbyte(static_cast<unsigned char>(l) & static_cast<unsigned char>(r));constexpr byte operator&(byte l, byte r) noexcept;-12- Effects: Equivalent to:
return static_cast<byte>(static_cast<unsigned char>(static_cast<unsigned int>(l) & static_cast<unsigned int>(r)))byte(static_cast<unsigned char>(l) & static_cast<unsigned char>(r));constexpr byte& operator^=(byte& l, byte r) noexcept;-13- Effects: Equivalent to:
return l = l ^ rbyte(static_cast<unsigned char>(l) ^ static_cast<unsigned char>(r));constexpr byte operator^(byte l, byte r) noexcept;-14- Effects: Equivalent to:
return static_cast<byte>(static_cast<unsigned char>(static_cast<unsigned int>(l) ^ static_cast<unsigned int>(r)))byte(static_cast<unsigned char>(l) ^ static_cast<unsigned char>(r));constexpr byte operator~(byte b) noexcept;-15- Effects: Equivalent to: return static_cast<byte>(static_cast<unsigned char>(~static_cast<unsigned int>(b)))
byte(~static_cast<unsigned char>(b));template <class IntType> constexpr IntType to_integer(byte b) noexcept;-16- Remarks: This function shall not participate in overload resolution unless is_integral_v<IntType> is true.
-17- Effects: Equivalent to: return static_cast<IntType>IntType(b);
[ 2016-07, Toronto Thursday night issues processing ]
Status to Ready
[ 2017-06-28, STL comments and provides wording ]
This proposed resolution performs its work in unsigned int, which is immune to promotion. For op=, I'm avoiding unnecessary verbosity.
It stylistically uses static_casts instead of functional-style casts. All of the static_casts are intentional, although not all of them are strictly necessary. I felt that it was simpler to always follow the same pattern for type conversions, instead of skipping static_casts by taking advantage of the possible ranges of values. (I could prepare an alternative PR to avoid unnecessary casts.) I'm not static_casting the shift arguments, because of how [expr.shift] works. For to_integer(), there's a tiny question. According to [expr.static.cast]/9, static_casting from [128, 255] bytes to signed (behavior) chars triggers unspecified behavior, whereas using unsigned char as an intermediate type would produce implementation-defined behavior, [conv.integral]/3. This question is basically theoretical, and it's unaffected by this proposed resolution (which is just changing a functional-style cast to a static_cast).[ 2017-06-27 P1 after 5 positive votes on c++std-lib. ]
The operations for std::byte ([support.types.byteops]) are currently specified to have undefined behaviour in general cases, since the type of the expression expr that appears in return byte(expr) is obtained by the arithmetic conversion rules and has higher conversion rank than unsigned char. Therefore, the value of the expression may be outside the range of the enum (for example, consider ~0), and by [expr.static.cast] p10 the conversion results in undefined behaviour.
I believe the original intent of the specification could be expressed correctly with the following, more verbose sequence of casts. I will only give one representative example:byte operator<<(byte b, IntType shift)Equivalent to: return byte(static_cast<unsigned char>(static_cast<unsigned char>(b) << shift));
History | |||
---|---|---|---|
Date | User | Action | Args |
2021-02-25 10:48:01 | admin | set | status: wp -> c++20 |
2017-11-13 19:01:36 | admin | set | status: voting -> wp |
2017-10-17 18:34:55 | admin | set | status: ready -> voting |
2017-07-16 20:11:05 | admin | set | messages: + msg9418 |
2017-07-16 20:11:05 | admin | set | status: new -> ready |
2017-06-28 17:52:24 | admin | set | messages: + msg9310 |
2017-06-28 17:52:24 | admin | set | messages: + msg9309 |
2017-06-27 21:42:35 | admin | set | messages: + msg9305 |
2017-03-24 00:00:00 | admin | create |