بنقرة واحدة
cpp
// Google's official C++ style guide. Covers headers, naming conventions, formatting, classes, memory management, RAII, smart pointers, and modern C++ features.
// Google's official C++ style guide. Covers headers, naming conventions, formatting, classes, memory management, RAII, smart pointers, and modern C++ features.
| name | cpp |
| description | Google's official C++ style guide. Covers headers, naming conventions, formatting, classes, memory management, RAII, smart pointers, and modern C++ features. |
Official Google C++ coding standards for consistent, maintainable code.
const liberally — for correctness and thread safetystd::unique_ptr and std::shared_ptrPROJECT_PATH_FILE_H_ format| Element | Convention | Example |
|---|---|---|
| Files | snake_case | url_table.cc, url_table.h |
| Classes/Structs | UpperCamelCase | UserService |
| Functions | UpperCamelCase | GetUserById |
| Variables | snake_case | user_count |
| Constants | kUpperCamelCase | kMaxRetries |
| Class members | snake_case_ | user_name_ (trailing _) |
| Macros | UPPER_SNAKE_CASE | MAX_BUFFER_SIZE |
// ✓ CORRECT - self-contained header with guard
#ifndef FOO_BAR_BAZ_H_
#define FOO_BAR_BAZ_H_
#include <string>
#include "base/basictypes.h"
class Baz {
public:
void DoSomething();
};
#endif // FOO_BAR_BAZ_H_
Include order: related header → C system → C++ stdlib → other libs → your headers.
// ✓ CORRECT
class MyClass {
public:
explicit MyClass(int value); // explicit for single-arg ctors
~MyClass();
void DoSomething();
int GetValue() const { return value_; }
private:
int value_;
std::string name_;
};
// ✓ CORRECT - unique_ptr for exclusive ownership
std::unique_ptr<Foo> FooFactory();
void FooConsumer(std::unique_ptr<Foo> ptr);
// ✓ CORRECT - shared_ptr for shared ownership
std::shared_ptr<const Foo> immutable_foo;
// ✗ INCORRECT - avoid raw new/delete
Foo* foo = new Foo(); // avoid
delete foo; // avoid
// ✓ CORRECT - use auto for complex types
auto it = my_map.find(key);
auto widget = std::make_unique<Widget>(arg1, arg2);
// ✓ CORRECT - range-based for loops
for (const auto& item : container) {
Process(item);
}
// ✓ CORRECT - nullptr, not NULL
Foo* ptr = nullptr;
// ✓ CORRECT - constexpr for compile-time constants
constexpr int kArraySize = 100;
// ✓ CORRECT - return type on same line
ReturnType ClassName::FunctionName(Type par_name1, Type par_name2) {
DoSomething();
return result;
}
// ✓ CORRECT - wrap long parameter lists (4-space indent)
ReturnType LongClassName::ReallyLongFunctionName(
Type par_name1,
Type par_name2,
Type par_name3) {
DoSomething();
}
// ✓ CORRECT - braces and spacing
if (condition) {
DoSomething();
} else {
DoSomethingElse();
}
// ✓ CORRECT - pointer/reference alignment (attached to type)
char* c;
const std::string& str;
| Mistake | Correct Approach |
|---|---|
| Using exceptions | Use error codes or absl::Status |
Bare new/delete | Use smart pointers (unique_ptr) |
NULL or 0 for null | Use nullptr |
| C-style casts | Use C++ casts (static_cast, etc.) |
using namespace std | Never in headers; avoid in .cc |
| Mutable global variables | Use singletons or dependency injection |
Missing explicit | Mark single-arg constructors explicit |
npx skills add testdino-hq/google-styleguides-skills/cpp
See cpp.md for complete details, examples, and edge cases.
Complete collection of Google's official style guides for 17 languages. Includes TypeScript, JavaScript, Python, Java, Go, C++, C#, Swift, Objective-C, HTML/CSS, AngularJS, Shell, R, Common Lisp, Vim Script, JSON, and Markdown. Production-ready coding standards used across Google's engineering organization, formatted for AI agent consumption.
Google's official AngularJS style guide. Covers controllers, services, directives, modules, dependency injection, and AngularJS 1.x best practices.
Google's official Common Lisp style guide. Covers naming conventions, formatting, macros, packages, documentation strings, and Lisp best practices.
Google's official C# style guide. Covers naming conventions, formatting, LINQ, async/await, XML documentation, and .NET best practices.
Google's official Go style guide. Covers gofmt formatting, naming conventions, error handling, interfaces, concurrency patterns, and package organization. Enforces idiomatic Go code with short variable names and explicit error checks.
Google's official HTML/CSS style guide. Covers formatting, naming, semantics, accessibility, and best practices for web markup and styling.