Back to Skills

cpp-templates-metaprogramming

verified

Use when creating generic and type-safe C++ libraries with templates, SFINAE, concepts, and compile-time metaprogramming.

View on GitHub

Marketplace

han

TheBushidoCollective/han

Plugin

jutsu-cpp

Technique

Repository

TheBushidoCollective/han
60stars

jutsu/jutsu-cpp/skills/cpp-templates-metaprogramming/SKILL.md

Last Verified

January 24, 2026

Install Skill

Select agents to install to:

Scope:
npx add-skill https://github.com/TheBushidoCollective/han/blob/main/jutsu/jutsu-cpp/skills/cpp-templates-metaprogramming/SKILL.md -a claude-code --skill cpp-templates-metaprogramming

Installation paths:

Claude
.claude/skills/cpp-templates-metaprogramming/
Powered by add-skill CLI

Instructions

# C++ Templates and Metaprogramming

Master C++ templates, template metaprogramming, SFINAE, concepts, and
compile-time computation. This skill enables you to create generic, type-safe,
and highly efficient C++ libraries with compile-time guarantees.

## Function Templates

### Basic Function Templates

```cpp
// Simple function template
template<typename T>
T max(T a, T b) {
    return (a > b) ? a : b;
}

// Usage
int i = max(10, 20);           // T = int
double d = max(3.14, 2.71);    // T = double
// auto x = max(10, 3.14);     // ERROR: can't deduce T

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

auto result = add(5, 3.14);    // T = int, U = double, returns double

// C++14: simpler return type deduction
template<typename T, typename U>
auto multiply(T a, U b) {
    return a * b;
}
```

### Template Specialization

```cpp
// Primary template
template<typename T>
T absolute(T value) {
    return (value < 0) ? -value : value;
}

// Full specialization for const char*
template<>
const char* absolute<const char*>(const char* value) {
    return value; // Strings don't have absolute value
}

// Full specialization for std::string
template<>
std::string absolute<std::string>(std::string value) {
    return value;
}

// Usage
int a = absolute(-5);              // Uses primary template
const char* b = absolute("test");  // Uses const char* specialization
```

### Function Template Overloading

```cpp
// Overload 1: Generic template
template<typename T>
void print(T value) {
    std::cout << "Generic: " << value << std::endl;
}

// Overload 2: Pointer specialization
template<typename T>
void print(T* ptr) {
    std::cout << "Pointer: " << *ptr << std::endl;
}

// Overload 3: Non-template overload
void print(const char* str) {
    std::cout << "String: " << str << std::endl;
}

// Usage
int x = 42;
print(x);       // Overload 1
print(&x);      // Overload 2
print("hello"); // Overload 3 (exa

Validation Details

Front Matter
Required Fields
Valid Name Format
Valid Description
Has Sections
Allowed Tools
Instruction Length:
23140 chars