coding-standard-cpp
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseC++ Coding Standards
C++编码规范
When reviewing or generating C++ code, follow these rules:
在评审或编写C++代码时,请遵循以下规则:
File Naming
文件命名
- Source files: snake_case or PascalCase with extension (e.g.,
.cpporuser_service.cpp)UserService.cpp - Header files: Same base name with ,
.h, or.hppextension.hxx - Template files: or
.tppfor template implementations.inl - Be consistent within a project
- 源文件: 使用snake_case或PascalCase命名,后缀为(例如:
.cpp或user_service.cpp)UserService.cpp - 头文件: 与源文件同名,后缀为、
.h或.hpp.hxx - 模板文件: 模板实现文件使用或
.tpp后缀.inl - 项目内保持一致
Header Guards / Pragma
头文件保护 / Pragma指令
- Prefer for modern compilers
#pragma once - Or use traditional guards: UPPER_SNAKE_CASE with suffix
_HPP
cpp
#pragma once
// or
#ifndef USER_SERVICE_HPP
#define USER_SERVICE_HPP
#endif- 现代编译器优先使用
#pragma once - 或使用传统保护方式: 采用UPPER_SNAKE_CASE命名并以结尾
_HPP
cpp
#pragma once
// or
#ifndef USER_SERVICE_HPP
#define USER_SERVICE_HPP
#endifNamespace Naming
命名空间命名
- Namespaces: all_lowercase or snake_case (e.g., ,
myproject)data_processing - Nested namespaces: Use C++17 syntax when possible (e.g., )
namespace project::utils {} - Avoid in headers
using namespace
- 命名空间: 全小写或snake_case(例如:、
myproject)data_processing - 嵌套命名空间: 尽可能使用C++17语法(例如:)
namespace project::utils {} - 避免在头文件中使用
using namespace
Variable Naming
变量命名
- Local variables: snake_case or camelCase (e.g., or
user_count)userCount - Member variables: Prefix with or suffix with
m_(e.g.,_orm_data)data_ - Static members: Prefix with (e.g.,
s_)s_instance - Constants: UPPER_SNAKE_CASE or kPascalCase (e.g., or
MAX_SIZE)kMaxSize - Global variables: Prefix with (e.g.,
g_)g_config
- 局部变量: snake_case或camelCase(例如:或
user_count)userCount - 成员变量: 前缀加或后缀加
m_(例如:_或m_data)data_ - 静态成员: 前缀加(例如:
s_)s_instance - 常量: UPPER_SNAKE_CASE或kPascalCase(例如:或
MAX_SIZE)kMaxSize - 全局变量: 前缀加(例如:
g_)g_config
Function/Method Naming
函数/方法命名
- Free functions: snake_case or camelCase (e.g., or
calculate_total())calculateTotal() - Member functions: camelCase or PascalCase (e.g., or
getUserId())GetUserId() - Getters/Setters: /
getprefix or just property name (e.g.,setorgetName())name() - Factory functions: ,
Create, orMakeprefix (e.g.,Build)CreateUser()
- 自由函数: snake_case或camelCase(例如:或
calculate_total())calculateTotal() - 成员函数: camelCase或PascalCase(例如:或
getUserId())GetUserId() - Getter/Setter方法: 使用/
get前缀或直接使用属性名(例如:set或getName())name() - 工厂函数: 使用、
Create或Make前缀(例如:Build)CreateUser()
Class/Type Naming
类/类型命名
- Classes: PascalCase (e.g., ,
UserService)DataProcessor - Structs: PascalCase (e.g., ,
Point)Rectangle - Interfaces: PascalCase with prefix optional (e.g.,
IorISerializable)Serializable - Template parameters: Single letter or PascalCase (e.g., ,
T,Container)KeyType - Enums: PascalCase for type, UPPER_SNAKE_CASE or PascalCase for values
- Type aliases: PascalCase (e.g., )
using StringList = std::vector<std::string>;
- 类: PascalCase(例如:、
UserService)DataProcessor - 结构体: PascalCase(例如:、
Point)Rectangle - 接口: PascalCase,可选加前缀(例如:
I或ISerializable)Serializable - 模板参数: 单个字母或PascalCase(例如:、
T、Container)KeyType - 枚举: 类型采用PascalCase,值采用UPPER_SNAKE_CASE或PascalCase
- 类型别名: PascalCase(例如:)
using StringList = std::vector<std::string>;
Smart Pointers
智能指针
- Use for single ownership
std::unique_ptr - Use for shared ownership
std::shared_ptr - Avoid raw /
newdelete
- 单一所有权场景使用
std::unique_ptr - 共享所有权场景使用
std::shared_ptr - 避免使用原生/
newdelete
Organization
代码组织
- Include guards / pragma once
- System includes (alphabetical)
- Project includes (alphabetical)
- Forward declarations
- Namespace opening
- Class declarations
- Inline/template implementations
- 头文件保护 / #pragma once
- 系统头文件(按字母排序)
- 项目头文件(按字母排序)
- 前置声明
- 命名空间开启
- 类声明
- 内联/模板实现