Commit 8218b857 authored by Chris Palmer's avatar Chris Palmer Committed by Commit Bot

Tweak the wording for Types in the style guide.

Add some color and examples.

Bug: None
Change-Id: Iacd9a21eeaf212d6b553f076bd27db36b544a3d0
Reviewed-on: https://chromium-review.googlesource.com/c/1483696Reviewed-by: default avatarJeremy Roman <jbroman@chromium.org>
Commit-Queue: Chris Palmer <palmer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#635327}
parent 074ad1d0
...@@ -178,31 +178,32 @@ Place platform-specific #includes in their own section below the "normal" ...@@ -178,31 +178,32 @@ Place platform-specific #includes in their own section below the "normal"
## Types ## Types
* Use `size_t` for object and allocation sizes, object counts, array and * Use `size_t` for object and allocation sizes, object counts, array and
pointer offsets, vector indices, and so on. The signed types are incorrect pointer offsets, vector indices, and so on. The signed types are
and unsafe for these purposes (e.g. integer overflow behavior for signed incorrect and unsafe for these purposes (e.g. integer overflow behavior for
types is undefined in the C and C++ standards, while the behavior is signed types is undefined in the C and C++ standards, while the behavior is
defined for unsigned types.) The C++ STL is a guide here: they use `size_t` defined for unsigned types). The C++ STL and libc are good guides here: they
and `foo::size_type` for very good reasons. use `size_t` and `foo::size_type` for very good reasons.
* Use `size_t` directly in preference to `std::string::size_type` and similar. * Use `size_t` directly in preference to `std::string::size_type` and similar.
* Occasionally classes may have a good reason to use a type other than `size_t` * Occasionally classes may have a good reason to use a type other than `size_t`
for one of these concepts, e.g. as a storage space optimization. In these for one of these concepts, e.g. as a storage space optimization. In these
cases, continue to use `size_t` in public-facing function declarations. cases, continue to use `size_t` in public-facing function declarations,
and continue to use unsigned types internally (e.g. `uint32_t`).
* Be aware that `size_t` (object sizes and indices), `off_t` (file offsets), * Be aware that `size_t` (object sizes and indices), `off_t` (file offsets),
`ptrdiff_t` (the difference between two pointer values), `intptr_t` (an `ptrdiff_t` (the difference between two pointer values), `intptr_t` (an
integer type large enough to hold the value of a pointer), `uint32_t`, integer type large enough to hold the value of a pointer), `uint32_t`,
`uint64_t`, and so on are not necessarily the same. Use the right type for `uint64_t`, and so on are not necessarily the same. Use the right type for
your purpose. your purpose. `CheckedNumeric` is an ergonomic way to perform safe
arithmetic and casting with and between these different types.
* Follow [Google C++ casting * Follow [Google C++ casting
conventions](https://google.github.io/styleguide/cppguide.html#Casting) conventions](https://google.github.io/styleguide/cppguide.html#Casting)
to convert arithmetic types when you know the conversion is safe. Use to convert arithmetic types when you know the conversion is safe. Use
`checked_cast<>()` (from `base/numerics/safe_conversions.h`) when you `checked_cast<T>` (from `base/numerics/safe_conversions.h`) when you need to
need to enforce via `CHECK()` that the source value is in-range for the `CHECK` that the source value is in range for the destination type. Use
destination type. Use `saturated_cast<>()` (from the same file) if you `saturated_cast<T>` if you instead wish to clamp out-of-range values.
instead wish to clamp out-of-range values.
* Do not use unsigned types to mean "this value should never be < 0". For * Do not use unsigned types to mean "this value should never be < 0". For
that, use assertions or run-time checks (as appropriate). that, use assertions or run-time checks (as appropriate).
...@@ -213,7 +214,7 @@ Place platform-specific #includes in their own section below the "normal" ...@@ -213,7 +214,7 @@ Place platform-specific #includes in their own section below the "normal"
* When passing values across network or process boundaries, use * When passing values across network or process boundaries, use
explicitly-sized types for safety, since the sending and receiving ends may explicitly-sized types for safety, since the sending and receiving ends may
not have been compiled with the same sizes for things like int and not have been compiled with the same sizes for things like `int` and
`size_t`. However, to the greatest degree possible, avoid letting these `size_t`. However, to the greatest degree possible, avoid letting these
sized types bleed through the APIs of the layers in question. sized types bleed through the APIs of the layers in question.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment