Files
gh-743175724-agents-project…/plugins/windows-development/skills/cpp-best-practices.md
2025-11-29 09:37:43 +08:00

1.2 KiB

name, description, version
name description version
C++最佳实践 现代C++编程规范和性能优化 1.0.0

C++ Best Practices Skill

Overview

Modern C++ programming best practices for high-performance Windows development.

Key Principles

RAII (Resource Acquisition Is Initialization)

class FileHandle {
    HANDLE handle_;
public:
    FileHandle(const wchar_t* path) {
        handle_ = CreateFileW(path, ...);
    }
    ~FileHandle() {
        if (handle_ != INVALID_HANDLE_VALUE) {
            CloseHandle(handle_);
        }
    }
    // Disable copying, enable moving
    FileHandle(const FileHandle&) = delete;
    FileHandle(FileHandle&& other) noexcept
        : handle_(other.handle_) {
        other.handle_ = INVALID_HANDLE_VALUE;
    }
};

Smart Pointers

  • Use std::unique_ptr for exclusive ownership
  • Use std::shared_ptr sparingly
  • Avoid std::weak_ptr unless breaking cycles

Modern C++ Features

  • Range-based for loops
  • Auto type deduction
  • Lambda expressions
  • Structured bindings (C++17)
  • Concepts (C++20)

Performance Tips

  1. Avoid unnecessary copying (use std::move)
  2. Reserve container capacity
  3. Use string_view for read-only strings
  4. Inline hot functions
  5. Profile before optimizing