Cpp

作者 mindrally97184105b5da無授權條款269 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫5 週前更新

Guidelines for modern C++ development with C++17/20 standards, memory safety, and performance optimization

AI 產生的概覽

提供現代 C++17/20 編碼規範,涵蓋記憶體安全、效能、測試與建置實務。

功能
提供一套 C++ 開發規範參考,涵蓋程式碼風格、命名慣例、智慧指標與 RAII、C++17 和 C++20 新特性、錯誤處理、效能、安全、並行、測試、文件與建置系統。它輸出的是建議性指引而非程式碼或檔案,說明諸如使用 std::unique_ptr、std::optional、concepts、ranges 和 std::span 等建議做法。同時建議採用 CMake、vcpkg 或 Conan、Google Test 或 Catch2 以及各類 sanitizer 等工具慣例。
適用情境
在撰寫、審查或重構 C++ 程式碼並希望遵循一致的現代 C++17/20 慣例時使用。也適合在決定 C++ 專案的記憶體所有權、錯誤處理、並行或建置與測試實務時參考。
執行需求
不需要指令碼或工具,僅為說明性文件。若要落實其建議,需要 C++17/20 工具鏈、CMake,以及選用的 Google Test、Catch2、vcpkg 或 Conan 等函式庫,但這些都未隨技能提供。

C++ Development Guidelines

You are an expert in modern C++ development with deep knowledge of C++17/20 standards, memory management, and high-performance programming.

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

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 m_ or use trailing underscore

Memory Management

Smart Pointers

  • Use std::unique_ptr for exclusive ownership
  • Use std::shared_ptr only when shared ownership is required
  • Use std::weak_ptr to break circular references
  • Avoid raw owning pointers

RAII (Resource Acquisition Is Initialization)

  • Use RAII for all resource management
  • Wrap resources in classes with proper destructors
  • Ensure exception safety through RAII
  • Use scope guards for cleanup operations

Best Practices

  • Prefer stack allocation over heap allocation
  • Use std::make_unique and std::make_shared
  • Avoid new and delete in application code
  • Use containers instead of raw arrays

Modern C++ Features

C++17 Features

  • Use structured bindings for tuple unpacking
  • Use std::optional for values that may not exist
  • Use std::variant for type-safe unions
  • Use if constexpr for compile-time conditionals
  • Use std::string_view for non-owning string references

C++20 Features

  • Use concepts for template constraints
  • Use ranges for cleaner algorithms
  • Use std::span for non-owning array views
  • Use coroutines for asynchronous operations
  • Use modules for faster compilation (when supported)

Error Handling

  • Use exceptions for error handling
  • Define custom exception types for domain-specific errors
  • Use noexcept for functions that don't throw
  • Catch exceptions by const reference
  • Provide strong exception guarantees where possible

Performance

  • Use const and constexpr liberally
  • Prefer move semantics with std::move
  • Use perfect forwarding with std::forward
  • Avoid unnecessary copies
  • Profile before optimizing
  • Use inline for small frequently-called functions

Security

Buffer Safety

  • Use std::array instead of C-style arrays
  • Use std::vector with bounds checking
  • Prefer std::string over C-style strings
  • Use std::span for array views

Type Safety

  • Avoid C-style casts; use static_cast, dynamic_cast, etc.
  • Use enum class instead of plain enums
  • Use nullptr instead of NULL
  • Enable compiler warnings and treat them as errors

Concurrency

  • Use std::thread and std::jthread for threading
  • Use std::mutex and std::lock_guard for synchronization
  • Use std::atomic for lock-free operations
  • Prefer std::async for simple async operations
  • Use condition variables for thread coordination

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

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

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

來源與署名

來源:mindrally/skills位於cpp提交9718410

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架