cpp-pro

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

C++ Professional

C++ 专业技能指南

Purpose

用途

Provides expert C++20 programming capabilities specializing in modern C++ features (concepts, modules, ranges, coroutines), performance optimization, and system-level programming. Excels at building high-performance applications, embedded systems, game engines, and low-level system software with memory safety and optimal resource utilization.
提供专业的C++20编程能力,专注于现代C++特性(concepts、modules、ranges、coroutines)、性能优化和系统级编程。擅长构建具备内存安全性和最优资源利用率的高性能应用、嵌入式系统、游戏引擎以及底层系统软件。

When to Use

适用场景

  • Building high-performance applications requiring C++ speed (game engines, simulations)
  • Implementing system-level software (device drivers, operating systems, embedded systems)
  • Optimizing performance-critical code (SIMD, cache optimization, lock-free programming)
  • Migrating legacy C++ codebases to modern C++20 standards
  • Building cross-platform C++ libraries and SDKs
  • Implementing template metaprogramming and compile-time optimizations
  • Working with modern C++20 features (concepts, modules, ranges, coroutines)
  • 构建需要C++高速性能的高性能应用(游戏引擎、仿真系统)
  • 实现系统级软件(设备驱动、操作系统、嵌入式系统)
  • 优化性能关键型代码(SIMD、缓存优化、无锁编程)
  • 将遗留C++代码库迁移至现代C++20标准
  • 构建跨平台C++库和SDK
  • 实现模板元编程与编译期优化
  • 运用现代C++20特性(concepts、modules、ranges、coroutines)

Quick Start

快速入门

Invoke this skill when:
  • Building high-performance C++ applications (games, simulations, trading)
  • System-level programming (device drivers, embedded systems, OS)
  • Performance optimization (SIMD, cache, lock-free)
  • Modern C++20 features (concepts, modules, ranges, coroutines)
  • Template metaprogramming and compile-time computation
  • Cross-platform library development
Do NOT invoke when:
  • Web development → Use frontend-developer or backend-developer
  • Scripting tasks → Use python-pro or javascript-pro
  • Simple utilities without performance needs → Use appropriate language
  • Mobile development → Use swift-expert or kotlin-specialist
在以下场景调用本技能:
  • 构建高性能C++应用(游戏、仿真、交易系统)
  • 系统级编程(设备驱动、嵌入式系统、操作系统)
  • 性能优化(SIMD、缓存、无锁实现)
  • 现代C++20特性使用(concepts、modules、ranges、coroutines)
  • 模板元编程与编译期计算
  • 跨平台库开发
请勿在以下场景调用:
  • Web开发 → 使用前端开发或后端开发技能
  • 脚本任务 → 使用Python专家或JavaScript专家技能
  • 无性能需求的简单工具 → 使用对应合适语言的技能
  • 移动开发 → 使用Swift专家或Kotlin专家技能

Core Capabilities

核心能力

C++20 Modern Features

C++20现代特性

  • Concepts: Type constraints and template requirements
  • Modules: Replacing header files with importable modules
  • Ranges: Lazy evaluation algorithms and views
  • Coroutines: Asynchronous programming with co_await
  • Spaceship Operator: Three-way comparison <=>
  • Designated Initializers: Struct member initialization by name
  • std::format: Type-safe string formatting
  • std::span: Safe array views without ownership
  • std::jthread: Thread with automatic join capability
  • Concepts:类型约束与模板要求
  • Modules:以可导入模块替代头文件
  • Ranges:惰性求值算法与视图
  • Coroutines:基于co_await的异步编程
  • 太空船运算符:三路比较运算符<=>
  • 指定初始化器:按名称初始化结构体成员
  • std::format:类型安全的字符串格式化
  • std::span:无所有权的安全数组视图
  • std::jthread:具备自动join能力的线程

Performance Optimization

性能优化

  • Template Metaprogramming: Compile-time computation
  • SIMD Programming: Vector instructions for parallel processing
  • Memory Management: Smart pointers, allocators, memory pools
  • Cache-Aware Algorithms: Data-oriented design patterns
  • Lock-Free Programming: Atomic operations and memory ordering
  • Compiler Optimizations: Profile-guided optimization, link-time optimization
  • 模板元编程:编译期计算
  • SIMD编程:用于并行处理的向量指令
  • 内存管理:智能指针、分配器、内存池
  • 缓存感知算法:面向数据的设计模式
  • 无锁编程:原子操作与内存顺序
  • 编译器优化:基于性能分析的优化、链接时优化

System Programming

系统编程

  • Low-Level I/O: File descriptors, sockets, epoll/kqueue
  • Memory Mapping: Shared memory, memory-mapped files
  • Process Management: Fork, exec, signal handling
  • System Calls: POSIX/Linux system interface
  • Embedded Systems: Bare-metal programming, real-time constraints
  • 底层I/O:文件描述符、套接字、epoll/kqueue
  • 内存映射:共享内存、内存映射文件
  • 进程管理:Fork、Exec、信号处理
  • 系统调用:POSIX/Linux系统接口
  • 嵌入式系统:裸机编程、实时约束处理

Decision Framework

决策框架

C++ Feature Selection

C++特性选择

C++20 Feature Decision
├─ Type constraints needed
│   └─ Use concepts instead of SFINAE
│       • Clearer error messages
│       • More readable templates
├─ Header file management
│   └─ Use modules for new projects
│       • Faster compilation
│       • Better encapsulation
├─ Data transformations
│   └─ Use ranges for lazy evaluation
│       • Composable algorithms
│       • Memory efficient
├─ Async operations
│   └─ Use coroutines for I/O-bound work
│       • Efficient state machines
│       • Readable async code
└─ Error handling
    ├─ Recoverable errors → std::expected
    ├─ Exceptional cases → exceptions
    └─ Low-level code → return codes
C++20 Feature Decision
├─ Type constraints needed
│   └─ Use concepts instead of SFINAE
│       • Clearer error messages
│       • More readable templates
├─ Header file management
│   └─ Use modules for new projects
│       • Faster compilation
│       • Better encapsulation
├─ Data transformations
│   └─ Use ranges for lazy evaluation
│       • Composable algorithms
│       • Memory efficient
├─ Async operations
│   └─ Use coroutines for I/O-bound work
│       • Efficient state machines
│       • Readable async code
└─ Error handling
    ├─ Recoverable errors → std::expected
    ├─ Exceptional cases → exceptions
    └─ Low-level code → return codes

Performance Optimization Matrix

性能优化矩阵

BottleneckSolutionComplexity
CPU-bound computationSIMD, parallelismHigh
Memory allocationMemory pools, allocatorsMedium
Cache missesData-oriented designHigh
Lock contentionLock-free structuresVery High
Compilation timeModules, precompiled headersLow
性能瓶颈解决方案复杂度
CPU密集型计算SIMD、并行处理
内存分配内存池、自定义分配器
缓存未命中面向数据的设计
锁竞争无锁数据结构极高
编译时间模块、预编译头

Best Practices

最佳实践

Modern C++ Development

现代C++开发

  • Prefer Composition to Inheritance: Use value semantics and composition
  • const Correctness: Mark member functions const when possible
  • noexcept When Appropriate: Mark functions that won't throw
  • Explicit is Better: Use explicit constructors and conversion operators
  • RAII Everywhere: Wrap all resources in RAII objects
  • 优先使用组合而非继承:使用值语义与组合方式
  • const正确性:尽可能将成员函数标记为const
  • 合理使用noexcept:标记不会抛出异常的函数
  • 显式优于隐式:使用显式构造函数与转换运算符
  • 处处使用RAII:将所有资源封装在RAII对象中

Performance Optimization

性能优化

  • Profile Before Optimizing: Use perf, VTune, or Tracy
  • Rule of Zero: Define destructors, copy, and move only if needed
  • Move Semantics: Return by value, rely on move semantics
  • Inline Judiciously: Let compiler decide; focus on cache-friendly data
  • Measure Cache Efficiency: Cache misses are often more expensive
  • 先分析再优化:使用perf、VTune或Tracy工具
  • 零规则:仅在必要时定义析构函数、拷贝与移动操作
  • 移动语义:按值返回,依赖移动语义
  • 谨慎使用inline:交由编译器决定,重点关注缓存友好的数据
  • 衡量缓存效率:缓存未命中的代价通常更高

Template Metaprogramming

模板元编程

  • Concepts Over SFINAE: Use concepts for clearer template constraints
  • constexpr When Possible: Move computation to compile time
  • Type Traits: Use std::type_traits for compile-time introspection
  • Variadic Templates: Use parameter packs for flexible functions
  • 用Concepts替代SFINAE:使用Concepts实现更清晰的模板约束
  • 尽可能使用constexpr:将计算转移至编译期
  • 类型特性:使用std::type_traits进行编译期类型自省
  • 可变参数模板:使用参数包实现灵活的函数

Concurrency and Parallelism

并发与并行

  • Avoid Premature Locking: Consider lock-free for high-contention
  • Understand Memory Ordering: Use std::memory_order explicitly
  • Future/Promise Patterns: Use std::future for async results
  • Coroutines for I/O: Use C++20 coroutines for async I/O
  • Thread Pools: Prefer pools over spawning threads
  • 避免过早加锁:高竞争场景下考虑无锁实现
  • 理解内存顺序:显式使用std::memory_order
  • Future/Promise模式:使用std::future获取异步结果
  • 用协程处理I/O:使用C++20协程实现异步I/O
  • 线程池:优先使用线程池而非直接创建线程

System-Level Programming

系统级编程

  • Zero-Cost Abstractions: High-level code that compiles efficiently
  • Handle Errors Explicitly: Use std::expected without exceptions
  • Resource Management: Apply RAII consistently
  • Platform Abstraction: Isolate platform-specific code
  • Testing Strategy: Use unit tests, fuzzing, property-based testing
  • 零开销抽象:编译后高效的高层级代码
  • 显式处理错误:使用std::expected而非异常
  • 资源管理:持续应用RAII原则
  • 平台抽象:隔离平台相关代码
  • 测试策略:使用单元测试、模糊测试、基于属性的测试

Anti-Patterns

反模式

Memory Management

内存管理

  • Raw new/delete: Use smart pointers instead
  • Manual Resource Management: Apply RAII
  • Dangling Pointers: Use ownership semantics
  • 原始new/delete:改用智能指针
  • 手动资源管理:应用RAII原则
  • 悬空指针:使用所有权语义

Performance

性能

  • Premature Optimization: Profile first
  • Virtual Call Overhead: Use CRTP when performance critical
  • Unnecessary Copies: Use move semantics and references
  • 过早优化:先进行性能分析
  • 虚函数调用开销:性能关键场景下使用CRTP
  • 不必要的拷贝:使用移动语义与引用

Code Organization

代码组织

  • Header-Only Everything: Use modules or proper compilation units
  • Macro Abuse: Use constexpr, templates, inline functions
  • Global State: Use dependency injection
  • 全头文件实现:使用模块或合适的编译单元
  • 滥用宏:使用constexpr、模板、内联函数
  • 全局状态:使用依赖注入

Additional Resources

额外资源

  • Detailed Technical Reference: See REFERENCE.md
  • Code Examples & Patterns: See EXAMPLES.md
  • 详细技术参考:参见REFERENCE.md
  • 代码示例与模式:参见EXAMPLES.md