cpp-rules

C++ 编码规范:覆盖风格、设计模式、安全性与测试。触发条件包括 .cpp、.cc、.cxx、.hpp、.h 文件、CMakeLists.txt、Makefile、GoogleTest 和 clang-tidy 相关内容。

仓库最近推送
GitHub Stars
180
Forks
21
Issues / PRs
0

安装方式

先用 Prompt 核对来源与依赖。也可下载安装包。

用 AI 助手安装

复制提示词,让助手先核对固定版本和依赖。

查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "cpp-rules":https://skillpi.com/zh/skills/deepskill-9c2efc9fb4961778600c3cc3d96435cfb70f8992c99df464106f5b6d5db66c96。安装前核对上游来源和版本:https://github.com/softspark/ai-toolkit。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。

来源与配置

github.com

配置方式待核实

请查看原始来源中的适用工具、依赖和权限要求。

安装实测待补充

C++ 编码规范:覆盖风格、设计模式、安全性与测试。触发条件包括 .cpp、.cc、.cxx、.hpp、.h 文件、CMakeLists.txt、Makefile、GoogleTest 和 clang-tidy 相关内容。

0 下载

介绍来源: third_party · softspark · 0.0.1 参考来源

SkillPi 解读

能完成的任务
翻译待完成,请参阅来源文档。
输入和输出
翻译待完成,请参阅来源文档。
实测状态
未测试

文件浏览

在原站浏览文件 ↗

原始 SKILL.md

打开原文 ↗
Frontmatter
name: cpp-rules
description: "C++ coding rules: style, patterns, security, testing. Triggers: .cpp, .cc, .cxx, .hpp, .h, CMakeLists.txt, Makefile, GoogleTest, clang-tidy."
effort: medium
user-invocable: false
allowed-tools: Read

C++ Rules

These rules come from app/rules/cpp/ in ai-toolkit. They cover the project's standards for coding style, frameworks, patterns, security, and testing in C++. Apply them when writing or reviewing C++ code.

C++ Coding Style

Naming

  • PascalCase: classes, structs, enums, type aliases, concepts.
  • camelCase or snake_case: functions, methods, variables (be consistent per project).
  • UPPER_SNAKE: macros, compile-time constants.
  • Prefix member variables with m_ or suffix with _ (pick one convention).
  • Namespace names: lowercase, short (namespace io, namespace util).

Modern C++ (17/20/23)

  • Use auto for iterator types and complex template deductions.
  • Use std::optional<T> instead of sentinel values or pointers for optional returns.
  • Use std::variant over union types. Use std::visit for dispatch.
  • Use std::string_view for non-owning string parameters.
  • Use structured bindings: auto [key, value] = *map.begin();.
  • Use constexpr for compile-time evaluation. Prefer over macros.

Memory Management

  • Use RAII exclusively. Every resource acquisition is an initialization.
  • Use std::unique_ptr for exclusive ownership (default choice).
  • Use std::shared_ptr only when ownership is genuinely shared.
  • Never use raw new/delete. Use std::make_unique / std::make_shared.
  • Use std::span<T> (C++20) for non-owning views over contiguous data.

Functions

  • Pass small types by value. Pass large types by const&.
  • Use [[nodiscard]] on functions whose return value must not be ignored.
  • Use noexcept on functions that do not throw (move constructors, destructors).
  • Limit function parameters to 4. Use structs for configuration objects.
  • Use trailing return types for complex template return deductions.

Includes and Dependencies

  • Use #pragma once or include guards. Prefer #pragma once for simplicity.
  • Order: corresponding header, C++ stdlib, third-party, project headers.
  • Forward-declare in headers when possible to reduce compile times.
  • Minimize header dependencies. Use the Pimpl idiom for ABI stability.

Avoid

  • Raw pointers for ownership. Use smart pointers.
  • C-style casts. Use static_cast, dynamic_cast, const_cast.
  • Macros for constants or functions. Use constexpr and templates.
  • using namespace std; in headers. Acceptable in .cpp files with caution.
  • std::endl -- use '\n' (endl flushes the buffer unnecessarily).

Formatting

  • Use clang-format with a committed .clang-format file.
  • Use clang-tidy for static analysis and automated modernization.
  • Max line length: 100-120 characters.
  • Braces: use Allman or K&R consistently per project.

C++ Frameworks

CMake

  • Use modern CMake (3.14+): target-based, not directory-based.
  • Use target_link_libraries with PUBLIC/PRIVATE/INTERFACE visibility.
  • Use FetchContent for dependency management. Avoid manual submodule vendoring.
  • Set CMAKE_CXX_STANDARD 20 (or 23) at the project level.
  • Use target_compile_options for per-target flags, not global add_compile_options.
  • Export targets with install(TARGETS ... EXPORT ...) for library consumers.

Boost

  • Use Boost.Asio for async networking and I/O.
  • Use boost::beast for HTTP/WebSocket built on Asio.
  • Use boost::json or nlohmann/json for JSON parsing.
  • Prefer C++ stdlib equivalents when available (e.g., std::optional over boost::optional).
  • Link only the Boost libraries you actually use. Many are header-only.

Qt

  • Use signals and slots for event-driven communication.
  • Use QObject parent-child ownership for automatic memory management.
  • Use QML for declarative UI. Keep business logic in C++ backend.
  • Use QThread with worker objects (moveToThread), not subclassing QThread.
  • Use smart pointers for non-QObject resources. QObject children are auto-deleted.

gRPC

  • Define services in .proto files. Generate C++ stubs with protoc.
  • Use async server with CompletionQueue for high-throughput services.
  • Use grpc::ClientContext for per-call deadlines and metadata.
  • Use interceptors for logging, auth, and metrics.
  • Set deadlines on every RPC call to prevent hanging.

Networking (Asio)

  • Use io_context as the event loop. Run from one or more threads.
  • Use co_await (C++20 coroutines) with Asio for clean async code.
  • Use strand for serializing access to shared state across handlers.
  • Use steady_timer for timeouts and periodic tasks.
  • Handle errors via error_code parameter, not exceptions, in async callbacks.

Database

  • Use libpq (PostgreSQL) or SOCI for database access.
  • Use prepared statements exclusively. Never concatenate SQL strings.
  • Use connection pooling for multi-threaded server applications.
  • Use SQLite via sqlite3 C API with RAII wrappers for embedded use cases.

Package Management

  • Use vcpkg or Conan 2 for dependency management.
  • Pin dependency versions in vcpkg.json or conanfile.py.
  • Use CI caching for build artifacts and dependency downloads.
  • Prefer pre-built binary packages for CI speed.

C++ Patterns

Error Handling

  • Use exceptions for truly exceptional conditions. Use return types for expected failures.
  • Use std::expected<T, E> (C++23) or Result<T, E> pattern for recoverable errors.
  • Use std::error_code / std::error_category for system-level errors.
  • Use noexcept on functions that must not throw (destructors, move operations).
  • Catch by const&. Never catch by value (slicing) or pointer.

RAII Patterns

  • Wrap every resource (memory, file, lock, socket) in an RAII type.
  • Use std::lock_guard or std::scoped_lock for mutex management.
  • Use std::unique_lock when deferred locking or condition variables are needed.
  • Use std::fstream (auto-closes) instead of fopen/fclose.
  • Write custom RAII wrappers for C library resources (file descriptors, handles).

Smart Pointer Patterns

  • unique_ptr: default ownership model. Transfer with std::move.
  • shared_ptr: use only for genuinely shared ownership graphs.
  • weak_ptr: break cycles in shared_ptr graphs. Use lock() to access.
  • Factory functions should return unique_ptr. Let callers upgrade to shared_ptr.
  • Never pass smart pointers by reference. Pass T& or T* to non-owning consumers.

Concurrency

  • Use std::thread with std::jthread (C++20) for auto-joining threads.
  • Use std::mutex + std::scoped_lock for shared data protection.
  • Use std::atomic<T> for lock-free single-variable synchronization.
  • Use std::condition_variable for producer-consumer patterns.
  • Use std::async / std::future for simple parallel computation.
  • Use std::counting_semaphore (C++20) for resource pool limiting.

Template Patterns

  • Use CRTP for compile-time polymorphism (static dispatch).
  • Use concepts (C++20) to constrain template parameters with clear error messages.
  • Use if constexpr for compile-time branching in templates.
  • Use variadic templates and fold expressions for parameter packs.
  • Prefer constexpr functions over template metaprogramming when possible.

Design Patterns

  • Use std::variant + std::visit for type-safe visitor pattern.
  • Use std::function for type-erased callbacks and strategy pattern.
  • Use Pimpl idiom (unique_ptr<Impl>) for ABI stability and compilation firewall.
  • Use Builder pattern with method chaining for complex object construction.
  • Use std::move semantics in move constructors for efficient resource transfer.

Anti-Patterns

  • Raw new/delete: use smart pointers and containers.
  • Returning raw pointers from factory functions: return unique_ptr.
  • const_cast to remove constness: redesign the interface.
  • Deep inheritance hierarchies: prefer composition and templates.
  • Premature optimization over readability: profile first, optimize second.

C++ Security

Buffer Overflow Prevention

  • Use std::string, std::vector, std::array instead of C arrays and char[].
  • Use std::span (C++20) for safe, bounds-checked views over contiguous data.
  • Never use strcpy, strcat, sprintf. Use std::string operations or snprintf.
  • Enable -D_FORTIFY_SOURCE=2 in release builds for runtime buffer checks.
  • Use at() for bounds-checked container access in untrusted input paths.

Memory Safety

  • Use smart pointers exclusively. Zero raw new/delete in application code.
  • Enable AddressSanitizer (-fsanitize=address) in development and CI builds.
  • Enable UndefinedBehaviorSanitizer (-fsanitize=undefined) in test builds.
  • Use -fstack-protector-strong for stack buffer overflow detection.
  • Use Valgrind for memory leak detection in integration tests.

Integer Safety

  • Check for overflow before arithmetic on untrusted integers.
  • Use std::numeric_limits<T>::max() for boundary checks.
  • Use unsigned types only for bit manipulation. Prefer signed for arithmetic.
  • Use static_cast explicitly. Never rely on implicit narrowing conversions.
  • Enable -Wconversion and -Wsign-conversion warnings.

Input Validation

  • Validate all external input: file data, network packets, command-line arguments.
  • Use std::stoi / std::stol with exception handling for string-to-number conversion.
  • Set maximum sizes for dynamic allocations based on untrusted input.
  • Validate file paths to prevent directory traversal (../).
  • Use allowlist validation for format specifiers and command strings.

Secure Coding

  • Use std::fill or explicit_bzero() to zero sensitive memory before deallocation.
  • Use constant-time comparison for secrets (avoid timing side-channels).
  • Use mlock() to prevent sensitive memory from being swapped to disk.
  • Compile with -fPIE -pie for position-independent executables (ASLR).
  • Enable -Werror in CI to prevent warnings from becoming vulnerabilities.

Dependencies

  • Audit third-party C libraries for known CVEs before inclusion.
  • Use vcpkg or Conan with pinned versions for reproducible builds.
  • Prefer well-maintained libraries with active security response teams.
  • Minimize C library usage. Prefer C++ standard library equivalents.

Concurrency Safety

  • Use std::mutex with std::scoped_lock for all shared data access.
  • Use std::atomic for lock-free single-variable operations.
  • Enable ThreadSanitizer (-fsanitize=thread) in test builds for race detection.
  • Avoid volatile for synchronization. It does not provide atomicity or ordering.
  • Use RAII lock guards. Never manually lock()/unlock().

Compiler Hardening

  • Enable all warnings: -Wall -Wextra -Wpedantic.
  • Use -D_GLIBCXX_ASSERTIONS for debug iterator and container checks.
  • Use -fno-exceptions only when exception safety is not required.
  • Link with -Wl,-z,relro,-z,now for full RELRO (GOT hardening).

C++ Testing

Framework

  • Use GoogleTest (gtest) as the primary test framework.
  • Use GoogleMock (gmock) for mocking interfaces and virtual classes.
  • Use Catch2 as a lightweight alternative (header-only, BDD-style).
  • Use CTest for test discovery and execution via CMake.

File Naming

  • Test files: foo_test.cpp or test_foo.cpp in a dedicated tests/ directory.
  • Mirror source directory structure in test directory.
  • One test file per source file or logical component.
  • Use CMakeLists.txt with add_test() to register tests.

Structure (GoogleTest)

  • Use TEST(SuiteName, TestName) for simple tests.
  • Use TEST_F(FixtureName, TestName) for tests sharing setup/teardown.
  • Use SetUp() / TearDown() in fixtures for per-test initialization.
  • Keep tests focused: one logical assertion per test case.

Assertions

  • Use EXPECT_* (non-fatal) by default. Use ASSERT_* only when continuation is meaningless.
  • EXPECT_EQ, EXPECT_NE, EXPECT_LT, EXPECT_GT for comparisons.
  • EXPECT_TRUE, EXPECT_FALSE for boolean conditions.
  • EXPECT_THROW(expr, ExceptionType) for exception testing.
  • EXPECT_THAT(value, matcher) with gmock matchers for complex assertions.

Parameterized Tests

  • Use INSTANTIATE_TEST_SUITE_P with testing::Values(...) for value-parameterized tests.
  • Use testing::Combine() for multi-dimensional parameterization.
  • Use TYPED_TEST_SUITE for type-parameterized tests across template types.
  • Prefer parameterized tests over copy-pasting similar test bodies.

Mocking (GoogleMock)

  • Define mock classes: MOCK_METHOD(ReturnType, MethodName, (Args), (Qualifiers)).
  • Use EXPECT_CALL(mock, Method(matchers)).WillOnce(Return(value)).
  • Use NiceMock<T> to suppress uninteresting call warnings.
  • Use StrictMock<T> to fail on any unexpected call.
  • Use dependency injection (constructor) to pass mock objects.

Build Integration

  • Use FetchContent or find_package to integrate gtest in CMake.
  • Enable BUILD_TESTING option to conditionally include tests.
  • Use ctest --output-on-failure for CI runs.
  • Use sanitizers in test builds: -fsanitize=address,undefined.

Best Practices

  • Test edge cases: empty input, max values, null pointers, boundary conditions.
  • Use RAII test fixtures for resource cleanup (no manual teardown).
  • Avoid testing private methods directly. Test through public API.
  • Use valgrind or ASan/UBSan in CI to detect memory errors.
  • Keep tests fast: mock I/O and external dependencies.

版本历史

  1. v6.2.0 GitHub ↗
    原版更新说明

    **Full Changelog**: https://github.com/softspark/ai-toolkit/compare/v6.1.0...v6.2.0

  2. v6.1.0 GitHub ↗
    原版更新说明

    **Full Changelog**: https://github.com/softspark/ai-toolkit/compare/v6.0.0...v6.1.0

  3. v6.0.0 GitHub ↗
    原版更新说明

    ## What's Changed * chore: bump softprops/action-gh-release from 3.0.3 to 3.0.4 by @dependabot[bot] in https://github.com/softspark/ai-toolkit/pull/45 * fix: handle Windows status output and clarify Antigravity workflow support by @softspark in https://github.com/softspark/ai-toolkit/pull/46 **Full Changelog**: https://github.com/softspark/ai-toolkit/compare/v5.5.0...v6.0.0

  4. v5.5.0 GitHub ↗
    原版更新说明

    ### Ecosystem - **OpenCode V2 and V1 compatibility:** `scripts/generate_opencode_plugin.py` emits the documented `id/setup/server` contract for V2 and V1 **1.18.29+**. Upgrade older V1 clients before using this plugin, then run `ai-toolkit update` to regenerate managed copies. `doctor` warns about obsolete V1-only exports. Scoped global/project IDs avoid native collisions; advisory work has one owner per location, while safety guards execute in every registered layer. The current V2 package is `@opencode/cli`; `opencode-ai` still publishes V1. - **Claude guard failure policy:** destructive-command, home-path and configuration guards use `onFailure: block` on Claude Code **2.1.295+**. Legacy entries migrate without duplication; hooks owned by other plugins survive. Older clients do not have this infrastructure-failure guarantee. - **Codex, Gemini and Devin:** Codex refresh accepts the two diagnostic model-request events. Gemini advisory hooks return native JSON and save native completion responses; Devin prompt/edit hooks emit event-labelled context. - **All 13 integrations reviewed:** refresh release metadata and the ecosystem snapshot, distinguish stable/preview channels, and pin Auggie's stable v0 references. User model, effort and approval selections remain unchanged. ### Fixed - **One Claude Stop checker:** remove the duplicate advisory handler from the global and app manifests. `quality-gate.sh` keeps PHPStan advisory through `quality-check.sh --php-only`, preserving checker precedence and Makefile settings. PHPStan now runs synchronously on Stop and TaskCompleted (#43). - **OpenCode guard execution:** replace the unsupported Bun stdin path with real subprocess execution in both APIs. Denial, missing guards, crashes and timeouts block tools. Transactional generation refuses user-owned collisions and symlinked destinations; lifecycle cleanup releases advisory ownership. - **Isolated release smoke:** `scripts/release.sh` installs the packed artifact in a disposable, network-disabled container with no mounts and the OS user's default home. Host settings are not used as a smoke sandbox. ### Changed - Hook entries: 29 to 28 across the same 14 Claude events. Skills remain 116 and agents remain 44. Bats coverage grows from 2012 to 2037 tests. - Refresh compatibility documentation and remove stale inventory counts from the backward-compatibility policy. All protected public surfaces remain. - Skill body budget is unchanged: the largest body remains `design-engineering` at 17,197 bytes, less than 2,000 bytes below the 18,000-byte warning threshold, so the release ratchet does not apply. **Included pull request:** [#43: run the project checker once on Claude Code Stop](https://github.com/softspark/ai-toolkit/pull/43), contributed by @jacekprzybyl. **Full changelog:** https://github.com/softspark/ai-toolkit/compare/v5.4.2...v5.5.0

  5. v5.4.2 GitHub ↗
    原版更新说明

    ## What's Changed * fix(install): read state.json on native Windows by @jacekprzybyl in https://github.com/softspark/ai-toolkit/pull/39 * test: isolate USERPROFILE together with HOME on Windows by @jacekprzybyl in https://github.com/softspark/ai-toolkit/pull/41 **Full Changelog**: https://github.com/softspark/ai-toolkit/compare/v5.4.1...v5.4.2

此处仅展示最近 5 个版本。 在原站查看完整版本历史 ↗

当前收录版本

0.0.1

核对来源版本 ↗

质量评测

满分100 · 来自 SkillPi LAB 实验室的评测报告。

场景价值85/100

亮点

  • C++ 编码规范覆盖全面,包含风格、现代特性、内存管理、框架、设计模式、安全和测试
  • 内容专业系统,对开发者具有显著实用价值,属于通用工具类别
触发精准90/100

亮点

  • 触发条件明确列出文件扩展名和工具名,覆盖合理且边界清晰
结构规范75/100

待提升项

  • 缺少 When to Use 和端到端示例 原报告严重程度:轻微

    文档结构清晰但缺少显式的使用场景说明和完整示例

    改进建议:建议补充 When to Use 章节和至少一个端到端应用示例

亮点

  • 文档分章节组织良好,frontmatter 完整,内容详实
执行可靠90/100

亮点

  • 纯文档型规则集,无外部工具依赖,建议具体可行,可靠性高
证据可验 (前四项均值)85/100

取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。

正文静态检测

100 / 100 · skillpi-20261123-no-r3-v1

正文未命中当前规则。

分数仅反映正文中的规则命中,供使用前参考。附件与运行行为不在此评分范围内。

权限与使用前检查

根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。

未从主文档识别到上述常见操作,不能据此判断无需权限或没有风险。

本站展示提示不会授予权限。涉及文件改动、密钥、外部发送或费用时,仍由你使用的工具按需确认。

安全评级与访问范围

safe · 静态证据核验,不代表运行安全保证。

使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。