isocpp / isocpp/CppCoreGuidelines
C++ integer promotion defeats "ES.101: Use unsigned types for bit manipulation" on small integer types
Nobody has claimed this yet.
- Dominant language
- CSS
- Stars
- 45.3k
- Forks
- 5.6k
- PR merge metrics
- No merged PRs in 30d
Description
I just fell on "Clang-Tidy: Use of a signed integer operand with a binary bitwise operator" warning.
Point is, I was writing 8bit unsigned integer expressions from my point of view, but expression below can involve signed arithmetic:
uint8_t{} | uint8_t{} | uint8_t{}
Which is not obvious.
Because of [over.built] and [conv.prom] sections, below assert is not guaranted to hold:
static_assert(std::is_same_v<decltype(std::declval<T>() | std::declval<T>()), T>)
And more importantly, this one does not hold either:
static_assert(std::is_signed_v< decltype(std::declval<T>() | std::declval<T>())> == std::is_signed_v<T>)
It currently fails on major implementations for uint8_t, uint_fast8_t, etc.
My limited understanding of the standard is that it could fail for any integer type smaller than int.
Technically the rule is correct, and I am not saying it should be changed to forbid bitwise ops on small ints.
But I think it reveals a quite uncomfortable situation that deserves a note.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the ES.101 rule and reading its surrounding guidance. Verify the small-integer promotion behavior against the cited [over.built] and [conv.prom] sections; done means the guideline clearly notes the mismatch between unsigned small operands and the promoted expression type.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 32/100