C++ Ecosystem

This skill should be used when working with C++ projects, CMakeLists.txt, Ninja, clang-tidy, clang-format, GoogleTest, Catch2, or Modern C++ (C++11-23) language patterns. Provides comprehensive C++ ecosystem patterns and best practices.

$ Instalar

git clone https://github.com/takeokunn/nixos-configuration /tmp/nixos-configuration && cp -r /tmp/nixos-configuration/home-manager/programs/claude-code/skills/cplusplus-ecosystem ~/.claude/skills/nixos-configuration

// tip: Run this command in your terminal to install the skill


name: C++ Ecosystem description: This skill should be used when working with C++ projects, CMakeLists.txt, Ninja, clang-tidy, clang-format, GoogleTest, Catch2, or Modern C++ (C++11-23) language patterns. Provides comprehensive C++ ecosystem patterns and best practices.

<cplusplus_language> <modern_features> <decision_tree name="when_to_use"> Are you working with modern C++ features like smart pointers, move semantics, or ranges? <if_yes>Apply modern C++ patterns for safer, more efficient code</if_yes> <if_no>Consider refactoring legacy code to modern C++ standards</if_no> </decision_tree>

template<Addable T> T add(T a, T b) { return a + b; }

// main.cpp import math; int main() { return add(1, 2); } Requires CMake 3.28+ with CMAKE_CXX_SCAN_FOR_MODULES

generator<int> range(int start, int end) { for (int i = start; i < end; ++i) { co_yield i; } }

task<int> async_compute() { co_return co_await some_async_operation(); } Requires coroutine library (cppcoro, libcoro, or custom promise types)

// Automatically generates ==, !=, <, >, <=, >=

std::variant<int, std::string, double> data = 42; std::visit([](auto&& val) { std::cout << val; }, data); </modern_features>

auto future = std::async(std::launch::async, []{ return 42; }); int result = future.get();

std::shared_mutex rw_mtx; std::shared_lock<std::shared_mutex> read_lock(rw_mtx); // multiple readers std::unique_lock<std::shared_mutex> write_lock(rw_mtx); // exclusive writer

// Waiting thread std::unique_lock<std::mutex> lock(mtx); cv.wait(lock, []{ return ready; });

// Notifying thread { std::lock_guard<std::mutex> lock(mtx); ready = true; } cv.notify_one();

<anti_patterns> Accessing shared data without synchronization Use mutex, atomic, or immutable data Circular lock dependencies Use std::scoped_lock for multiple mutexes, consistent lock ordering </anti_patterns>

// source class Widget::Impl { // implementation details }; <use_case>Reducing compile times, ABI stability, hiding implementation</use_case>

class Derived : public Base<Derived> { public: void implementation() { /_ ... _/ } }; <use_case>Static polymorphism, mixin classes, compile-time polymorphism</use_case>

<anti_patterns> Using raw pointers for ownership Use std::unique_ptr or std::shared_ptr

<projectstructure> <standard_layout> . ├── CMakeLists.txt ├── cmake/ │ └── modules/ ├── src/ │ ├── CMakeLists.txt │ ├── main.cpp │ └── lib/ ├── include/ │ └── project/ ├── tests/ │ ├── CMakeLists.txt │ └── test*.cpp └── build/ </standard_layout> </project_structure>

<cmake_patterns> Target-based CMake (3.0+) cmake_minimum_required(VERSION 3.20) project(MyProject VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF)

add_library(mylib STATIC src/mylib.cpp) target_include_directories(mylib PUBLIC include) target_compile_features(mylib PUBLIC cxx_std_20)

add_executable(myapp src/main.cpp) target_link_libraries(myapp PRIVATE mylib)

target_link_libraries(myapp PRIVATE Threads::Threads) target_link_libraries(mytests PRIVATE GTest::gtest GTest::gtest_main)

<clang_tidy> Static analysis and linting tool clang-tidy src/*.cpp -- -std=c++20

WarningsAsErrors: '' HeaderFilterRegex: '.'

CheckOptions:

  • key: readability-identifier-naming.ClassCase value: CamelCase
  • key: readability-identifier-naming.FunctionCase value: camelBack
  • key: readability-identifier-naming.VariableCase value: lower_case

<common_checks> Replace NULL with nullptr Use auto where appropriate Use override keyword Convert C-style loops to range-based Check ownership semantics Detect use-after-move bugs Detect unnecessary copies </common_checks> </clang_tidy>

<clangformat> Code formatting tool clang-format -i src/.cpp include/_.hpp

<cmake_preset>

CMakePresets.json sanitizer configuration

{ "configurePresets": [{ "name": "sanitize", "cacheVariables": { "CMAKE_CXX_FLAGS": "-fsanitize=address,undefined -fno-omit-frame-pointer" } }] } </cmake_preset>

add_executable(tests tests/test_main.cpp) target_link_libraries(tests PRIVATE GTest::gtest GTest::gtest_main)

include(GoogleTest) gtest_discover_tests(tests) </cmake_integration>

TEST(MyTest, BasicAssertion) { EXPECT_EQ(1 + 1, 2); }

TEST(MyTest, StringComparison) { std::string s = "hello"; EXPECT_STREQ(s.c_str(), "hello"); }

class MyFixture : public ::testing::Test { protected: void SetUp() override { /_ setup / } void TearDown() override { / cleanup _/ } };

TEST_F(MyFixture, FixtureTest) { EXPECT_TRUE(true); }

add_executable(tests tests/test_main.cpp) target_link_libraries(tests PRIVATE Catch2::Catch2WithMain)

include(CTest) include(Catch) catch_discover_tests(tests) </cmake_integration>

TEST_CASE("Basic arithmetic", "[math]") { REQUIRE(1 + 1 == 2); CHECK(2 * 2 == 4); }

TEST_CASE("String operations", "[string]") { std::string s = "hello";

SECTION("length") {
    REQUIRE(s.length() == 5);
}

SECTION("comparison") {
    REQUIRE(s == "hello");
}

}

<context7_integration> Use Context7 MCP for up-to-date C++ documentation

<cplusplus_libraries> </cplusplus_libraries>

<usage_patterns> resolve-library-id libraryName="cppreference" get-library-docs context7CompatibleLibraryID="/websites/cppreference_com" topic="std::unique_ptr"

<best_practices> Use smart pointers instead of raw pointers for ownership Enable -Wall -Wextra -Werror for all builds Run clang-tidy before committing Format with clang-format for consistent style Prefer const correctness throughout Use noexcept for move constructors and destructors Prefer constexpr for compile-time computation Use std::string_view for non-owning string references Prefer range-based for loops over index-based Use structured bindings for tuple/pair access Document public API with Doxygen comments Write unit tests alongside implementation </best_practices>

<error_escalation> Compiler warning about unused variable Fix warning, maintain clean build Compilation error Fix error, verify with full build Memory leak or undefined behavior detected Stop, require safe memory management Buffer overflow or security vulnerability Block operation, require immediate fix </error_escalation>

<related_agents> Architecture design for C++ class hierarchies and template metaprogramming Doxygen documentation and API reference generation CMake configuration and C++ implementation tasks Debugging memory leaks, segfaults, and undefined behavior </related_agents>

<related_skills> Symbol operations for C++ code navigation and refactoring C++ documentation via /websites/cppreference_com and CMake docs via /Kitware/CMake Debugging with sanitizers, valgrind, and gdb Creating library documentation with Doxygen </related_skills>