mcpField
C
cpp

cpp

github.com/htlin222開発78130 回インストール4 週間前に更新
リポジトリ

cpp について

cppは、モダンなC++コードの作成と保守を支援するスキルです。メモリ安全性、テンプレート、性能改善、レガシーコードの近代化をパターンと実践的な指針で扱います。

主な機能

  1. 01スマートポインターとRAIIの指針
  2. 02STLコンテナとアルゴリズムの実践
  3. 03C++20 conceptsとテンプレート
  4. 04メモリや並行処理の問題への対処

活用シーン

  1. 01メモリリークやダングリングポインターの調査
  2. 02レガシーC++コードの近代化
  3. 03コード最適化とデータ競合への対処

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/htlin222/dotfiles --skill cpp

インストール後、Claude Code で/cppを入力して読み込まれたか確認します。

詳細

C++ 開発

安全でパフォーマンスに優れたモダンな C++ コードを記述します。

使用する場面

  • C++ コードの記述
  • メモリ管理の問題
  • テンプレートメタプログラミング
  • パフォーマンスの最適化
  • レガシー C++ のモダナイズ

モダン C++ のパターン

スマートポインター
// Unique ownership
auto ptr = std::make_unique<Resource>();
process(std::move(ptr));

// Shared ownership
auto shared = std::make_shared<Resource>();
auto copy = shared;  // Reference count: 2

// Weak reference (no ownership)
std::weak_ptr<Resource> weak = shared;
if (auto locked = weak.lock()) {
    // Use locked
}
RAII
class FileHandle {
    FILE* handle_;
public:
    explicit FileHandle(const char* path)
        : handle_(fopen(path, "r")) {
        if (!handle_) throw std::runtime_error("Failed to open");
    }
    ~FileHandle() { if (handle_) fclose(handle_); }

    // Rule of 5
    FileHandle(const FileHandle&) = delete;
    FileHandle& operator=(const FileHandle&) = delete;
    FileHandle(FileHandle&& other) noexcept
        : handle_(std::exchange(other.handle_, nullptr)) {}
    FileHandle& operator=(FileHandle&& other) noexcept {
        std::swap(handle_, other.handle_);
        return *this;
    }
};
コンテナーとアルゴリズム
std::vector<int> nums = {3, 1, 4, 1, 5};

// Prefer algorithms over raw loops
std::sort(nums.begin(), nums.end());

auto it = std::find_if(nums.begin(), nums.end(),
    [](int n) { return n > 3; });

// Range-based for
for (const auto& num : nums) {
    std::cout << num << '\n';
}

// Structured bindings (C++17)
std::map<std::string, int> scores;
for (const auto& [name, score] : scores) {
    std::cout << name << ": " << score << '\n';
}
テンプレート
// Concepts (C++20)
template<typename T>
concept Numeric = std::integral<T> || std::floating_point<T>;

template<Numeric T>
T sum(const std::vector<T>& values) {
    return std::accumulate(values.begin(), values.end(), T{});
}

// SFINAE (pre-C++20)
template<typename T,
    typename = std::enable_if_t<std::is_arithmetic_v<T>>>
T multiply(T a, T b) { return a * b; }

ベストプラクティス

  • const と constexpr を優先します
  • 生ポインターよりスマートポインターを使用します
  • Rule of 0/5 に従います
  • STL アルゴリズムを優先します
  • 読み取り専用の文字列には std::string_view を使用します
  • 警告を有効にします:-Wall -Wextra -Wpedantic

よくある問題

問題症状修正
メモリーリークメモリーの増加スマートポインターを使用します
ダングリングポインタークラッシュ/UBライフタイムを確認します
バッファーオーバーフロークラッシュ/セキュリティ上の問題std::vector/span を使用します
データ競合状態の不整合mutex/atomic

例

入力:「メモリーリークを修正」 アクション: 生ポインターをスマートポインターに置き換え、RAII を確実に適用します

入力:「この C++ コードをモダナイズ」 アクション: C++17/20 の機能を適用し、STL を使用して安全性を高めます

コメント