Dear C++ arbiters,
How the rules have always been explained to me, and what I enforced in my Readability reviews, is roughly:
- Don't use unsigned types merely to document that a value never goes negative; the value of that documentation does not exceed the downsides of unsigned types.
- However, if you are working with cases where size_t would be the language's natural type anyway -- that is, object lengths in memory, e.g. vector::size(), string::size(), sizeof expr, "length of data packet", etc. -- you are welcome to use size_t (and, in general, should do so, to reduce casting at e.g. STL boundaries, and to avoid potential truncation problems using int on a 64-bit machine).
However, I've gotten pushback from both directions in code reviews:
- Despite the phrase "you are welcome to use size_t", some authors have been reluctant to allow size_t at all. (This view is distressingly prevalent in google3 itself, where I've had changes rejected because they added any use of size_t. In Chromium, I've always been able to get these accepted, but sometimes needed more explanation.)
- In the change that triggered this email ( https://codereview.chromium.org/2255403002/ ), replacing size_t with int has been controversial because the concept in question is "the size of something" (in this case, the number of pixels on one side of a square image). In other words, the author's contention is that if the meaning of the variable includes the concept of something's size, size_t should be allowed. (Evan, please clarify if I misrepresented you.)
I have previously sought to have C-style clarify the style guide, but have not gotten anywhere. I believe the style arbiters feel things are sufficiently clear, or else they are reluctant to change text that has been around a long time.
An alternative is that we add language to the Chromium style guide clarifying this, e.g. examples of good and bad usage. This might be useful, but I'm wary of adding more content to that document, especially when it's not actually an exception to the Google style rules. Perhaps modifying the "C++ Do's and Dont's" page would be better, but that page says it's non-binding on authors.
So the questions are:
- Is there rough consensus that the above interpretation of the rules is accurate? Do we need to start a chromium-dev thread for this?
- Should we add language to clarify this somewhere? If so where?
PK