string_view
operator+
vs. StrCat()
absl::Status
std::bind
absl::optional
and std::unique_ptr
absl::StrFormat()
make_unique
and private
Constructors.bool
explicit
= delete
)switch
Statements Responsibly= delete
AbslHashValue
and Youcontains()
std::optional
parametersif
and switch
statements with initializersinline
Variablesstd::unique_ptr
Must Be MovedAbslStringify()
vector.at()
auto
for Variable DeclarationsBackground: About five years ago, within Google we started publishing a series of C++ tips, about once a week, that became known as the “C++ Tips of the Week” (TotW). They became wildly successful, and we are still publishing them to this day (indicating that a language as rich as C++ will not deplete us of topics anytime soon).
Not only do we discuss the finer points of the language, but in true “tip” fashion, offer our advice or design preferences. The collective set of C++ TotW has become a canon within Google itself, cited thousands of times per week in code reviews and internal mailing list discussions. Often they are cited by number, and some have become known simply as “totw/110” or “totw/77”.
We’ve decided to expose most of these tips to the Abseil development community, and the C++ community at large. Over the coming months, we’ll be posting new tips as we review and refine them for publication. Not all the entries we’ve written are relevant to the outside world; some were specific to our internal tools and abstractions, and some have become obsolete as the language has changed. But we’ll publish what we can and what we find valuable (and write new tips!) on this page as they become available.
Note: we will be keeping the original numbering scheme on these tips, and
original publication date, so that the 12K or so people that have some exposure
to the original numbering don't have to learn new citations. As a result, some
tips may appear missing and/or out of order to a casual reader. But rest
assured, we're giving you the good stuff.
Some tips may include historical information that, though accurate, may reflect
philosophy and/or usage at the time the tip was originally written. In most
cases, we have updated that information to reflect current practices, and note
exceptions that are historical, where applicable.
Because these tips are being published out of original order, we’ve listed them below in the order of re-publication.
Tip of the Week #231: Between Here and There: Some Minor Overlooked Algorithms
Tip of the Week #232: When to Use auto
for Variable Declarations
Tip of the Week #234: Pass by Value, by Pointer, or by Reference?
Tip of the Week #197: Reader Locks Should Be Rare
Tip of the Week #224: Avoid vector.at()
Tip of the Week #227: Be Careful with Empty Containers and Unsigned Arithmetic
Tip of the Week #229: Ranked Overloads for Template Metaprogramming
Tip of the Week #198: Tag Types
Tip of the Week #218: Designing Extension Points With FTADLE
Tip of the Week #124: absl::StrFormat()
Tip of the Week #18: String Formatting with Substitute
Tip of the Week #215: Stringifying Custom Types with AbslStringify()
Tip of the Week #3: String Concatenation and operator+
vs. StrCat()
Tip of the Week #188: Be Careful With Smart-Pointer Function Parameters
Tip of the Week #186: Prefer to Put Functions in the Unnamed Namespace
Tip of the Week #187: std::unique_ptr
Must Be Moved
Tip of the Week #116: Keeping References on Arguments
Tip of the Week #165: if
and switch
statements with initializers
Tip of the Week #181: Accessing the value of a StatusOr<T>
Tip of the Week #5: Disappearing Act
Tip of the Week #76: Use absl::Status
Tip of the Week #140: Constants: Safe Idioms
Tip of the Week #5: Disappearing Act
Tip of the Week #163: Passing std::optional
parameters
Tip of the Week #171: Avoid Sentinel Values
Tip of the Week #172: Designated Initializers
Tip of the Week #173: Wrapping Arguments in Option Structs
Tip of the Week #175: Changes to Literal Constants in C++14 and C++17.
Tip of the Week #176: Prefer Return Values to Output Parameters
Tip of the Week #177: Assignability vs. Data Member Types
Tip of the Week #108: Avoid std::bind
Tip of the Week #146: Default vs Value Initialization
Tip of the Week #166: When a Copy is not a Copy
Tip of the Week #161: Good Locals and Bad Locals
Tip of the Week #168: inline
Variables
Tip of the Week #158: Abseil Associative containers and contains()
Tip of the Week #180: Avoiding Dangling References
Tip of the Week #182: Initialize Your Ints!
Tip of the Week #147: Use Exhaustive switch
Statements Responsibly
Tip of the Week #103: Flags Are Globals
Tip of the Week #45: Avoid Flags, Especially in Library Code
Tip of the Week #90: Retired Flags
Tip of the Week #153: Don't Use using-directives
Tip of the Week #136: Unordered Containers
Tip of the Week #144: Heterogeneous Lookup in Associative Containers
Tip of the Week #152: AbslHashValue
and You
Tip of the Week #117: Copy Elision and Pass-by-value
Tip of the Week #148: Overload Sets
Tip of the Week #149: Object Lifetimes vs. = delete
Tip of the Week #11: Return Policy
Tip of the Week #120: Return Values are Untouchable
Tip of the Week #24: Copies, Abbrv.
Tip of the Week #141: Beware Implicit Conversions to bool
Tip of the Week #143: C++11 Deleted Functions (= delete
)
Tip of the Week #134: make_unique
and private
Constructors.
Tip of the Week #61: Default Member Initializers
Tip of the Week #93: using absl::Span
Tip of the Week #142: Multi-parameter Constructors and explicit
Tip of the Week #88: Initialization: =, (), and {}
Tip of the Week #10: Splitting Strings, not Hairs
Tip of the Week #3: String Concatenation and operator+ vs. StrCat()
Tip of the Week #36: New Join API
Tip of the Week #59: Joining Tuples
Tip of the Week #42: Prefer Factory Functions to Initializer Methods
Tip of the Week #74: Delegating and Inheriting Constructors
Tip of the Week #123: absl::optional
and std::unique_ptr
Tip of the Week #131: Special Member Functions and `= default`
Tip of the Week #119: Using-declarations and namespace aliases
Tip of the Week #130: Namespace Naming
Tip of the Week #109: Meaningful `const` in Function Declarations
Tip of the Week #126: `make_unique` is the new `new`
Tip of the Week #112: emplace vs. push_back
Tip of the Week #65: Putting Things in their Place
Tip of the Week #99: Nonmember Interface Etiquette
Tip of the Week #135: Test the Contract, not the Implementation
Tip of the Week #49: Argument-Dependent Lookup
Tip of the Week #101: Return Values, References, and Lifetimes
Tip of the Week #107: Reference Lifetime Extension
Tip of the Week #64: Raw String Literals
Tip of the Week #86: Enumerating with Class
Tip of the Week #94: Callsite Readability and bool Parameters
Tip of the Week #122: Test Fixtures, Clarity, and Dataflow
Tip of the Week #55: Name Counting and unique_ptr
Tip of the Week #77: Temporaries, Moves, and Copies