Command Palette

Search for a command to run...

UnylyUnyly
Назад к скиллам

cpp-templates-metaprogramming

БесплатноБез исполняемых скриптовНе проверен

Use when C++ templates and metaprogramming including template specialization, SFINAE, type traits, and C++20 concepts.

TheBushidoCollectiveот TheBushidoCollective★ 202 звезды на GitHub

Об этом скилле

C++ Templates and Metaprogramming

Template metaprogramming enables compile-time computation and code generation, creating flexible, efficient abstractions without runtime overhead. This skill covers function and class templates, specialization, SFINAE, type traits, and modern concepts-based template constraints.

Function Templates

Function templates enable writing generic algorithms that work with any type satisfying requirements.

#include <iostream>
#include <vector>
#include <string>

// Basic function template
template<typename T>
T maximum(T a, T b) {
    return (a > b) ? a : b;
}

// Multiple template parameters
template<typename T, typename U>
auto add(T a, U b) -> decltype(a + b) {
    return a + b;
}

// Template with non-type parameters
template<typename T, size_t N>
size_t array_size(T (&)[N]) {
    return N;
}

// Template overloading
template<typename T>
void print(T value) {
    std::cout << value << "\n";
}

template<typename T>
void print(const std::vector<T>& vec) {
    for (const auto& item : vec) {
        std::cout << item << " ";
    }
    std::cout << "\n";
}

void function_template_examples() {
    auto max_int = maximum(10, 20);
    auto max_double = maximum(3.14, 2.71);
    auto max_string = maximum(std::string("abc"), std::string("xyz"));

    auto sum = add(5, 3.14);  // int + double

    int arr[] = {1, 2, 3, 4, 5};
    std::cout << "Array size: " << array_size(arr) << "\n";

    print(42);
    print(std::vector<int>{1, 2, 3});
}

Class Templates

Class templates enable creating generic containers and data structures.

#include <iostream>
#include <stdexcept>

// Basic class template
template<typename T>
class Stack {
    T* data_;
    size_t size_;
    size_t capacity_;

public:
    Stack(size_t capacity = 10)
        : data_(new T[capacity])
        , size_(0)
        , capacity_(capacity) {}

    ~Stack() {
        delete[] data_;
    }

    void push(const T& value) {
        if (size_ >= capacity_) {
            resize();
        }
        data_[size_++] = value;
    }

    T pop() {
        if (size_ == 0) {
            throw std::underflow_error("Stack is empty");
        }
        return data_[--size_];
    }

    bool empty() const { return size_ == 0; }
    size_t size() const { return size_; }

private:
    void resize() {
        capacity_ *= 2;
        T* new_data = new T[capacity_];
        for (size_t i = 0; i < size_; ++i) {
            new_data[i] = data_[i];
        }
        delete[] data_;
        data_ = new_data;
    }
};

// Multiple template parameters
template<typename Key, typename Value>
class Pair {
    Key key_;
    Value value_;

public:
    Pair(const Key& k, const Value& v) : key_(k), value_(v) {}

    const Key& key() const { return key_; }
    const Value& value() const { return value_; }
};

// Template with default parameters
template<typename T, typename Allocator = std::allocator<T>>
class Vector {
    // Implementation
};

void class_template_examples() {
    Stack<int> int_stack;
    int_stack.push(1);
    int_stack.push(2);
    std::cout << int_stack.pop() << "\n";

    Stack<std::string> str_stack;
    str_stack.push("hello");

    Pair<std::string, int> p("age", 30);
}

Template Specialization

Template specialization allows providing custom implementations for specific types.

#include <iostream>
#include <cstring>

// Primary template
template<typename T>
class Container {
    T value_;

public:
    Container(const T& value) : value_(value) {}

    void print() const {
        std::cout << "Generic: " << value_ << "\n";
    }

    size_t memory_size() const {
        return sizeof(T);
    }
};

// Full specialization for const char*
template<>
class Container<const char*> {
    const char* value_;

public:
    Container(const char* value) : value_(value) {}

    void print() const {
        std::cout << "C-string: " << value_ << "\n";
    }

    size_t memory_size() const {
        return std::strlen(value_) + 1;
    }
};

// Partial specialization for pointers
template<typename T>
class Container<T*> {
    T* value_;

public:
    Container(T* value) : value_(value) {}

    void print() const {
        std::cout << "Pointer: " << *value_ << "\n";
    }

    size_t memory_size() const {
        return sizeof(T*);
    }
};

// Function template specialization
template<typename T>
bool is_negative(T value) {
    return value < 0;
}

template<>
bool is_negative<bool>(bool value) {
    return false;  // bool can't be negative
}

void specialization_examples() {
    Container<int> c1(42);
    c1.print();  // Generic

    Container<const char*> c2("hello");
    c2.print();  // C-string

    int x = 10;
    Container<int*> c3(&x);
    c3.print();  // Pointer
}

SFINAE (Substitution Failure Is Not An Error)

SFINAE enables compile-time function selection based on type properties.

#include <iostream>
#include <type_traits>
#include <vector>

// Enable if type has begin() and end()
templat
Полный SKILL.md на GitHub

Установить cpp-templates-metaprogramming в Claude Code и Claude Desktop

Зарегайся, чтобы установить скилл

Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.

  • Открой команду установки в одну строку
  • Сохраняй скиллы в синхронизируемую библиотеку
  • Уведомления, когда скиллы обновляются
Зарегаться бесплатноУ меня уже есть аккаунт

Разрешённые инструменты

Инструменты, которые скиллу разрешено вызывать.

Read
Write
Edit
Grep
Glob
Bash

FAQ

Что делает скилл cpp-templates-metaprogramming?

Use when C++ templates and metaprogramming including template specialization, SFINAE, type traits, and C++20 concepts.

Как установить скилл cpp-templates-metaprogramming?

Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.

Скилл cpp-templates-metaprogramming запускает скрипты?

Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.

Похожие скиллы

Сравнить cpp-templates-metaprogramming с