[llvm-dev] [RFC] Coding Standards: "prefer `int` for regular arithmetic, use `unsigned` only for bitmask and when you intend to rely on wrapping behavior."

David Greene via llvm-dev llvm-dev at lists.llvm.org
Tue Jun 11 09:58:58 PDT 2019

James Henderson via llvm-dev <llvm-dev at lists.llvm.org> writes:

> Maybe it's just because I work in code around the binary file formats
> almost exclusively, but unsigned (or more often uint64_t) is FAR more
> common than int everywhere I go. I don't have time right now to read
> up on the different links you provided, and I expect this is covered
> in them, but it also seems odd to me to use int in a loop when
> indexing in a container (something that can't always be avoided),
> given the types of size() etc.

The reason to use int as an index in loops is for its algebraic
properties, which allows some optimizations (vectorization, for
example), where unsigned would cause problems.

Basically, the optimizer can assume overflow is undefined behavior and
optimize accordingly.

+10000 for preferring signed unless there's a good reason for unsigned.


More information about the llvm-dev mailing list