1 min read

Google's C++ Standarts Cheatsheet

Google's C++ Standarts Cheatsheet
Photo by Rajeshwar Bachu / Unsplash

This post is a brief of "A Quick Summary of Google C++ Style Guide". If you want a post with better quailty, and more detail, please check the original post.

  1. The header include order: Related header, C library, C++ library, other libraries' .h, your project's .h.
  2. Do not use "using namespace xxx" syntax in headers.
  3. Use .cc instead of .cpp.
  4. Variables needed for if, while and for statements should normally be declared within those statements, so that such variables are confined to those scopes.
  5. Constructors should never call virtual functions.
  6. Use a struct only for passive objects that carry data; everything else is a class.
  7. Prefer small and focused functions. If a function exceeds about 40 lines, think about whether it can be broken up without harming the structure of the program.
  8. Within function input parameter lists all references must be const, while output arguments are pointers.
  9. Never use std::auto_ptr. Instead, use std::unique_ptr.

Naming Rules

File names: all lowercase and can include underscores (_) or dashes (-). For example,
my_useful_class.cc.

Type names: start with a capital letter and have a capital letter for each new word, with no underscores:
MyExcitingClass
MyExcitingEnum

Variable names: all lowercase, with underscores between words. Data members of classes (but not structs) additionally have trailing underscores. For instance,
a_local_variable
a_struct_data_member
a_class_data_member_

Constants: named with a leading "k" followed by mixed case. Underscores can be used as separators in the rare cases where capitalization cannot be used for separation. For instance,
const int kDaysInAWeek = 7;
const int kAndroid8_0_0 = 24;  // Android 8.0.0

Functions: regular functions have mixed case; accessors and mutators may be named like variables.

Namespace: all lower-case. Top-level namespace names are based on the project name.

Enumerate: mixed type like constants. For instance,
enum UrlTableErrors

Macros: all upper case letters, include underscores. For instance,
#define PI_ROUNDED 3.0