AgentSkillsCN

m15-anti-pattern

常见的 C++ 反模式。触发点:全局变量、new/delete、C 风格的强制类型转换、宏、void*。

SKILL.md
--- frontmatter
name: m15-anti-pattern
description: "Common C++ Anti-Patterns. Triggers: global variables, new/delete, C-style cast, macros, void*."

C++ Anti-Patterns

Core Question

Is this C or C++?

  • C-Style: malloc, free, (int)x, void*.
  • C++ Style: std::vector, std::unique_ptr, static_cast, templates.

Error → Design Question

IssueDesign Question
Hard to refactorAre you using Macros?
LeakAre you using new?
UBAre you using reinterpret_cast?

Thinking Prompt

  1. Can I delete this new?

    • Use make_unique.
  2. Can I remove this macro?

    • Use constexpr or templates.

Quick Reference

Anti-PatternModern Fix
new Tmake_unique<T>
T* ownershipunique_ptr<T>
(T)ptrstatic_cast<T>(ptr)
#defineconstexpr