coding-standard-cpp

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

C++ Coding Standards

C++编码规范

When reviewing or generating C++ code, follow these rules:
在评审或编写C++代码时,请遵循以下规则:

File Naming

文件命名

  • Source files: snake_case or PascalCase with
    .cpp
    extension (e.g.,
    user_service.cpp
    or
    UserService.cpp
    )
  • Header files: Same base name with
    .h
    ,
    .hpp
    , or
    .hxx
    extension
  • Template files:
    .tpp
    or
    .inl
    for template implementations
  • Be consistent within a project
  • 源文件: 使用snake_case或PascalCase命名,后缀为
    .cpp
    (例如:
    user_service.cpp
    UserService.cpp
  • 头文件: 与源文件同名,后缀为
    .h
    .hpp
    .hxx
  • 模板文件: 模板实现文件使用
    .tpp
    .inl
    后缀
  • 项目内保持一致

Header Guards / Pragma

头文件保护 / Pragma指令

  • Prefer
    #pragma once
    for modern compilers
  • Or use traditional guards: UPPER_SNAKE_CASE with
    _HPP
    suffix
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
#endif

Namespace 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
    using namespace
    in headers
  • 命名空间: 全小写或snake_case(例如:
    myproject
    data_processing
  • 嵌套命名空间: 尽可能使用C++17语法(例如:
    namespace project::utils {}
  • 避免在头文件中使用
    using namespace

Variable Naming

变量命名

  • Local variables: snake_case or camelCase (e.g.,
    user_count
    or
    userCount
    )
  • Member variables: Prefix with
    m_
    or suffix with
    _
    (e.g.,
    m_data
    or
    data_
    )
  • Static members: Prefix with
    s_
    (e.g.,
    s_instance
    )
  • Constants: UPPER_SNAKE_CASE or kPascalCase (e.g.,
    MAX_SIZE
    or
    kMaxSize
    )
  • Global variables: Prefix with
    g_
    (e.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.,
    calculate_total()
    or
    calculateTotal()
    )
  • Member functions: camelCase or PascalCase (e.g.,
    getUserId()
    or
    GetUserId()
    )
  • Getters/Setters:
    get
    /
    set
    prefix or just property name (e.g.,
    getName()
    or
    name()
    )
  • Factory functions:
    Create
    ,
    Make
    , or
    Build
    prefix (e.g.,
    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
    I
    prefix optional (e.g.,
    ISerializable
    or
    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
    std::unique_ptr
    for single ownership
  • Use
    std::shared_ptr
    for shared ownership
  • Avoid raw
    new
    /
    delete
  • 单一所有权场景使用
    std::unique_ptr
  • 共享所有权场景使用
    std::shared_ptr
  • 避免使用原生
    new
    /
    delete

Organization

代码组织

  • Include guards / pragma once
  • System includes (alphabetical)
  • Project includes (alphabetical)
  • Forward declarations
  • Namespace opening
  • Class declarations
  • Inline/template implementations
  • 头文件保护 / #pragma once
  • 系统头文件(按字母排序)
  • 项目头文件(按字母排序)
  • 前置声明
  • 命名空间开启
  • 类声明
  • 内联/模板实现