Google C++ Style Guide
This skill provides the complete Google C++ Style Guide for C++20. Apply these rules when writing or reviewing C++ code.
Quick Reference
| Topic |
Key Rules |
| Naming |
Types: PascalCase, functions: PascalCase, variables: snake_case, constants: kPascalCase, macros: UPPER_CASE |
| Formatting |
80 char lines, 2-space indent, spaces (not tabs), { on same line |
| Headers |
Self-contained, #define guards, include what you use |
| Classes |
Prefer composition over inheritance, explicit constructors, private data members |
Core Principles
- Optimize for the reader - Code is read more than written
- Be consistent - Follow existing patterns in the codebase
- Avoid surprising constructs - Prefer clear over clever
Target Version
Code should target C++20. Do not use C++23 features or non-standard extensions.
Detailed References
For complete rules on specific topics:
- Header Files - Include guards, ordering, forward declarations
- Scoping - Namespaces, linkage, static/global variables
- Classes - Constructors, inheritance, operator overloading
- Functions - Parameters, overloading, default arguments
- C++ Features - Smart pointers, exceptions, RTTI, lambdas
- Naming - All naming conventions with examples
- Comments - Documentation style and requirements
- Formatting - Whitespace, braces, line length
Common Patterns
Header File Template
#ifndef PROJECT_PATH_FILE_H_
#define PROJECT_PATH_FILE_H_
#include "project/public/header.h"
#include <sys/types.h>
#include <string>
#include <vector>
#include "other/library.h"
#include "project/internal.h"
namespace project {
class MyClass {
public:
MyClass();
explicit MyClass(int value);
void DoSomething();
int count() const { return count_; }
void set_count(int count) { count_ = count; }
private:
int count_;
};
} // namespace project
#endif // PROJECT_PATH_FILE_H_
Class Declaration Order
class MyClass {
public:
// Types and type aliases
using ValueType = int;
// Static constants
static constexpr int kMaxSize = 100;
// Constructors and assignment
MyClass();
MyClass(const MyClass&) = default;
MyClass& operator=(const MyClass&) = default;
// Destructor
~MyClass();
// All other methods
void Process();
protected:
// Protected members (if needed)
private:
// Private methods
void InternalHelper();
// Data members
int value_;
};
Function Comments
// Returns an iterator positioned at the first entry >= `start_word`.
// Returns nullptr if no such entry exists.
// The client must not use the iterator after the table is destroyed.
std::unique_ptr<Iterator> GetIterator(absl::string_view start_word) const;
Critical Rules Summary
Always Do
- Use
#define guards in all headers
- Make single-argument constructors
explicit
- Use
nullptr (not NULL or 0) for pointers
- Use
override or final for virtual function overrides
- Declare data members
private
- Initialize variables at declaration
Never Do
- Use
using namespace directives
- Use C-style casts (use
static_cast, etc.)
- Use exceptions (in Google code)
- Use non-standard extensions
- Define macros in headers (when possible)
- Use
std::auto_ptr (use std::unique_ptr)
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: google-cpp-style3description: Google C++ Style Guide rules for writing clean, maintainable C++ code. Use when writing C++, reviewing code, discussing naming conventions, formatting, class design, or any C++ best practices. Covers headers, scoping, classes, functions, naming, comments, and formatting. Use when this capability is needed.4---56# Google C++ Style Guide78This skill provides the complete Google C++ Style Guide for C++20. Apply these rules when writing or reviewing C++ code.910## Quick Reference1112| Topic | Key Rules |13|-------|-----------|14| **Naming** | Types: `PascalCase`, functions: `PascalCase`, variables: `snake_case`, constants: `kPascalCase`, macros: `UPPER_CASE` |15| **Formatting** | 80 char lines, 2-space indent, spaces (not tabs), `{` on same line |16| **Headers** | Self-contained, `#define` guards, include what you use |17| **Classes** | Prefer composition over inheritance, explicit constructors, `private` data members |1819## Core Principles20211. **Optimize for the reader** - Code is read more than written222. **Be consistent** - Follow existing patterns in the codebase233. **Avoid surprising constructs** - Prefer clear over clever2425## Target Version2627Code should target **C++20**. Do not use C++23 features or non-standard extensions.2829## Detailed References3031For complete rules on specific topics:3233- [Header Files](headers.md) - Include guards, ordering, forward declarations34- [Scoping](scoping.md) - Namespaces, linkage, static/global variables35- [Classes](classes.md) - Constructors, inheritance, operator overloading36- [Functions](functions.md) - Parameters, overloading, default arguments37- [C++ Features](features.md) - Smart pointers, exceptions, RTTI, lambdas38- [Naming](naming.md) - All naming conventions with examples39- [Comments](comments.md) - Documentation style and requirements40- [Formatting](formatting.md) - Whitespace, braces, line length4142## Common Patterns4344### Header File Template4546```cpp47#ifndef PROJECT_PATH_FILE_H_48#define PROJECT_PATH_FILE_H_4950#include "project/public/header.h"5152#include <sys/types.h>5354#include <string>55#include <vector>5657#include "other/library.h"58#include "project/internal.h"5960namespace project {6162class MyClass {63 public:64 MyClass();65 explicit MyClass(int value);6667 void DoSomething();68 int count() const { return count_; }69 void set_count(int count) { count_ = count; }7071 private:72 int count_;73};7475} // namespace project7677#endif // PROJECT_PATH_FILE_H_78```7980### Class Declaration Order8182```cpp83class MyClass {84 public:85 // Types and type aliases86 using ValueType = int;8788 // Static constants89 static constexpr int kMaxSize = 100;9091 // Constructors and assignment92 MyClass();93 MyClass(const MyClass&) = default;94 MyClass& operator=(const MyClass&) = default;9596 // Destructor97 ~MyClass();9899 // All other methods100 void Process();101102 protected:103 // Protected members (if needed)104105 private:106 // Private methods107 void InternalHelper();108109 // Data members110 int value_;111};112```113114### Function Comments115116```cpp117// Returns an iterator positioned at the first entry >= `start_word`.118// Returns nullptr if no such entry exists.119// The client must not use the iterator after the table is destroyed.120std::unique_ptr<Iterator> GetIterator(absl::string_view start_word) const;121```122123## Critical Rules Summary124125### Always Do126127- Use `#define` guards in all headers128- Make single-argument constructors `explicit`129- Use `nullptr` (not `NULL` or `0`) for pointers130- Use `override` or `final` for virtual function overrides131- Declare data members `private`132- Initialize variables at declaration133134### Never Do135136- Use `using namespace` directives137- Use C-style casts (use `static_cast`, etc.)138- Use exceptions (in Google code)139- Use non-standard extensions140- Define macros in headers (when possible)141- Use `std::auto_ptr` (use `std::unique_ptr`)142143---144> Converted and distributed by [TomeVault](https://tomevault.io/claim/clickhouse) — claim your Tome and manage your conversions.145<!-- tomevault:4.0:skill_md:2026-04-11 -->