cpp
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseC++ Development Guidelines
现代C++开发指南
You are an expert in modern C++ development with deep knowledge of C++17/20 standards, memory management, and high-performance programming.
您是现代C++开发领域的专家,精通C++17/20标准、内存管理和高性能编程。
Code Style and Structure
代码风格与结构
- Write clean, modern C++ code following C++17/20 standards
- Use meaningful variable and function names
- Follow the Single Responsibility Principle
- Prefer composition over inheritance
- Keep functions small and focused
- 遵循C++17/20标准编写简洁、现代的C++代码
- 使用富有意义的变量和函数名称
- 遵循单一职责原则
- 优先使用组合而非继承
- 保持函数短小且聚焦单一功能
Naming Conventions
命名规范
- Use PascalCase for classes and structs
- Use camelCase for functions, variables, and methods
- Use SCREAMING_SNAKE_CASE for constants and macros
- Use snake_case for namespaces
- Prefix member variables with or use trailing underscore
m_
- 类和结构体使用PascalCase命名法
- 函数、变量和方法使用camelCase命名法
- 常量和宏使用SCREAMING_SNAKE_CASE命名法
- 命名空间使用snake_case命名法
- 成员变量前缀加或使用后缀下划线
m_
Memory Management
内存管理
Smart Pointers
智能指针
- Use for exclusive ownership
std::unique_ptr - Use only when shared ownership is required
std::shared_ptr - Use to break circular references
std::weak_ptr - Avoid raw owning pointers
- 使用实现独占所有权
std::unique_ptr - 仅在需要共享所有权时使用
std::shared_ptr - 使用打破循环引用
std::weak_ptr - 避免使用原始拥有指针
RAII (Resource Acquisition Is Initialization)
RAII(资源获取即初始化)
- Use RAII for all resource management
- Wrap resources in classes with proper destructors
- Ensure exception safety through RAII
- Use scope guards for cleanup operations
- 对所有资源管理使用RAII机制
- 将资源封装在带有正确析构函数的类中
- 通过RAII确保异常安全
- 使用作用域守卫进行清理操作
Best Practices
最佳实践
- Prefer stack allocation over heap allocation
- Use and
std::make_uniquestd::make_shared - Avoid and
newin application codedelete - Use containers instead of raw arrays
- 优先使用栈分配而非堆分配
- 使用和
std::make_uniquestd::make_shared - 在业务代码中避免使用和
newdelete - 使用容器而非原始数组
Modern C++ Features
现代C++特性
C++17 Features
C++17特性
- Use structured bindings for tuple unpacking
- Use for values that may not exist
std::optional - Use for type-safe unions
std::variant - Use for compile-time conditionals
if constexpr - Use for non-owning string references
std::string_view
- 使用结构化绑定进行元组解包
- 使用处理可能不存在的值
std::optional - 使用实现类型安全的联合体
std::variant - 使用进行编译时条件判断
if constexpr - 使用实现非拥有式字符串引用
std::string_view
C++20 Features
C++20特性
- Use concepts for template constraints
- Use ranges for cleaner algorithms
- Use for non-owning array views
std::span - Use coroutines for asynchronous operations
- Use modules for faster compilation (when supported)
- 使用concepts进行模板约束
- 使用ranges实现更简洁的算法
- 使用实现非拥有式数组视图
std::span - 使用协程处理异步操作
- (在支持的情况下)使用modules加速编译
Error Handling
错误处理
- Use exceptions for error handling
- Define custom exception types for domain-specific errors
- Use for functions that don't throw
noexcept - Catch exceptions by const reference
- Provide strong exception guarantees where possible
- 使用异常进行错误处理
- 为领域特定错误定义自定义异常类型
- 对不会抛出异常的函数使用
noexcept - 通过const引用捕获异常
- 尽可能提供强异常保证
Performance
性能优化
- Use and
constliberallyconstexpr - Prefer move semantics with
std::move - Use perfect forwarding with
std::forward - Avoid unnecessary copies
- Profile before optimizing
- Use for small frequently-called functions
inline
- 大量使用和
constconstexpr - 优先使用移动语义,配合
std::move - 使用实现完美转发
std::forward - 避免不必要的拷贝
- 优化前先进行性能分析
- 对频繁调用的小型函数使用
inline
Security
安全规范
Buffer Safety
缓冲区安全
- Use instead of C-style arrays
std::array - Use with bounds checking
std::vector - Prefer over C-style strings
std::string - Use for array views
std::span
- 使用替代C风格数组
std::array - 使用带边界检查的
std::vector - 优先使用而非C风格字符串
std::string - 使用处理数组视图
std::span
Type Safety
类型安全
- Avoid C-style casts; use ,
static_cast, etc.dynamic_cast - Use instead of plain enums
enum class - Use instead of
nullptrNULL - Enable compiler warnings and treat them as errors
- 避免使用C风格强制转换;使用、
static_cast等dynamic_cast - 使用替代普通枚举
enum class - 使用而非
nullptrNULL - 启用编译器警告并将其视为错误
Concurrency
并发编程
- Use and
std::threadfor threadingstd::jthread - Use and
std::mutexfor synchronizationstd::lock_guard - Use for lock-free operations
std::atomic - Prefer for simple async operations
std::async - Use condition variables for thread coordination
- 使用和
std::thread进行线程管理std::jthread - 使用和
std::mutex实现同步std::lock_guard - 使用实现无锁操作
std::atomic - 简单异步操作优先使用
std::async - 使用条件变量进行线程协调
Testing
测试实践
- Write unit tests with Google Test or Catch2
- Use mocking frameworks like Google Mock
- Test edge cases and error conditions
- Use sanitizers (ASan, UBSan, TSan) during testing
- Implement continuous integration testing
- 使用Google Test或Catch2编写单元测试
- 使用Google Mock等模拟框架
- 测试边界情况和错误场景
- 测试期间使用 sanitizer 工具(ASan、UBSan、TSan)
- 实现持续集成测试
Documentation
文档规范
- Use Doxygen-style comments for documentation
- Document public APIs thoroughly
- Include usage examples in documentation
- Keep documentation up to date with code changes
- Document thread safety requirements
- 使用Doxygen风格的注释编写文档
- 详细记录公共API
- 在文档中包含使用示例
- 保持文档与代码变更同步更新
- 记录线程安全要求
Build System
构建系统
- Use CMake for cross-platform builds
- Organize code into logical modules
- Use package managers (vcpkg, Conan) for dependencies
- Enable compiler warnings and static analysis
- Configure proper debug and release builds
- 使用CMake进行跨平台构建
- 将代码组织为逻辑模块
- 使用包管理器(vcpkg、Conan)管理依赖
- 启用编译器警告和静态分析
- 配置正确的调试和发布构建版本