##### C++ Tips

###### TotW #1: string_view

###### TotW #3: String Concatenation and operator+ vs. StrCat()

###### TotW #5: Disappearing Act

###### TotW #5: Disappearing Act

###### TotW #10: Splitting Strings, not Hairs

###### TotW #11: Return Policy

###### TotW #24: Copies, Abbrv.

###### TotW #36: New Join API

###### TotW #42: Prefer Factory Functions to Initializer Methods

###### TotW #45: Avoid Flags, Especially in Library Code

###### TotW #49: Argument-Dependent Lookup

###### TotW #55: Name Counting and unique_ptr

###### TotW #59: Joining Tuples

###### TotW #61: Default Member Initializers

###### TotW #64: Raw String Literals

###### TotW #65: Putting Things in their Place

###### TotW #74: Delegating and Inheriting Constructors

###### TotW #76: Use absl::Status

###### TotW #77: Temporaries, Moves, and Copies

###### TotW #86: Enumerating with Class

###### TotW #88: Initialization: =, (), and {}

###### TotW #90: Retired Flags

###### TotW #93: using absl::Span

###### TotW #94: Callsite Readability and bool Parameters

###### TotW #99: Nonmember Interface Etiquette

###### TotW #101: Return Values, References, and Lifetimes

###### TotW #103: Flags Are Globals

###### TotW #107: Reference Lifetime Extension

###### TotW #108: Avoid

`std::bind`

###### TotW #109: Meaningful `const` in Function Declarations

###### TotW #112: emplace vs. push_back

###### TotW #116: Keeping References on Arguments

###### TotW #117: Copy Elision and Pass-by-value

###### TotW #119: Using-declarations and namespace aliases

###### TotW #120: Return Values are Untouchable

###### TotW #122: Test Fixtures, Clarity, and Dataflow

###### TotW #123:

`absl::optional`

and`std::unique_ptr`

###### TotW #126: `make_unique` is the new `new`

###### TotW #130: Namespace Naming

###### TotW #131: Special Member Functions and `= default`

###### TotW #134:

`make_unique`

and`private`

Constructors.###### TotW #135: Test the Contract, not the Implementation

###### TotW #136: Unordered Containers

###### TotW #140: Constants: Safe Idioms

###### TotW #141: Beware Implicit Conversions to

`bool`

###### TotW #142: Multi-parameter Constructors and

`explicit`

###### TotW #143: C++11 Deleted Functions (

`= delete`

)###### TotW #144: Heterogeneous Lookup in Associative Containers

###### TotW #146: Default vs Value Initialization

###### TotW #147: Use Exhaustive

`switch`

Statements Responsibly###### TotW #148: Overload Sets

###### TotW #149: Object Lifetimes vs.

`= delete`

###### TotW #152:

`AbslHashValue`

and You###### TotW #153: Don't Use using-directives

###### TotW #161: Good Locals and Bad Locals

###### TotW #163: Passing

`absl::optional`

parameters###### TotW #165:

`if`

and`switch`

statements with initializers###### TotW #166: When a Copy is not a Copy

###### TotW #168:

`inline`

Variables###### TotW #171: Avoid Sentinel Values

###### TotW #172: Designated Initializers

###### TotW #173: Wrapping Arguments in Option Structs

###### TotW #175: Changes to Literal Constants in C++14 and C++17.

###### TotW #176: Prefer Return Values to Output Parameters

###### TotW #177: Assignability vs. Data Member Types

###### TotW #181: Accessing the value of a StatusOr<T>

###### TotW #186: Prefer to Put Functions in the Unnamed Namespace

###### TotW #187:

`std::unique_ptr`

Must Be Moved

Originally posted as TotW #175 on January 30, 2020

Updated 2020-04-06

Quicklink: abseil.io/tips/175

“The only thing that stays the same is change” – Melissa Etheridge

C++ now has some features that can make numeric literals more readable.

Integer literals can now be written in binary (`0b00101010`

) as well as the
decimal (`42`

), hex (`0x2A`

), and octal (`052`

) formats that have been supported
since the dawn of time.

Single quotes (`'`

) serve as digit separators, and can be used to group digits
in numeric literals of any base (`0xDEAD'C0DE`

).

Floating point literals can be specified in hex (`0x2A0p-4`

).

Binary literals such as `0b1110'0000`

can be more readable than hex (the next
best option) when manipulating bit sets or working with low-level protocols.
Code that does these things should be rare in google3, where a higher-level
technique is usually a better choice.

C++14 allows a single quote (`'`

) to be used to group digits in a numeric
literal. These digit separators have no effect on the value of the constant:
their only function is to help readers. They can make it easy to see at a glance
how many digits are present, and know that none are missing. For example,
`1'000'000'000`

is more obviously one billion than is `1000000000`

(and unlike
`1e9`

it is an integer, not a floating point value).

There are no restrictions on the number of digits per group, not even a
requirement of consistency within a literal: `0b1'001'0001`

is a valid way to
write the number 145 (and may even make sense for a byte that is interpreted as
three separate fields).

While most *decimal* floating point literals are not exactly representable in
the binary floating point formats used by most computers, *hex* floating
literals do map directly to bit patterns in floating point so long as enough
bits are available. This avoids rounding errors when converting a literal to
floating point format (though truncation errors are still possible if too many
hex digits are present).

Hex floating point literals are indicated by writing a `p`

(or `P`

) to separate
the significand from the exponent—where decimal floating point literals would
use `e`

(or `E`

). For example, `0x2Ap12`

is another way to write the value ```
0x2A
<< 12
```

, i.e., 0x2A000. The exponent is always written in decimal, denotes a
power of 2, and may be negative: `0x1p-10`

is (exactly) `1.0/1024`

.

- Binary literals should be used sparingly, for code which cares about bit manipulation.
- Consider using digit separators when a numeric literal is too long to take in at a single glance.
- When using digit separators, use conventional group sizes:
- For decimal, use groups of three digits unless there is a conflicting convention (as with some currencies, for example).
- For binary, prefer groups of four bits (nibble) or eight bits (octet/byte) unless the digits have a more semantically significant grouping.
- For hex, use groups of 2, 4 or 8 hex digits.