cpp-templates-metaprogramming
БесплатноБез исполняемых скриптовНе проверенUse when creating generic and type-safe C++ libraries with templates, SFINAE, concepts, and compile-time metaprogramming.
от TheBushidoCollective★ 202 звезды на GitHubОб этом скилле
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
// 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
// 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
// 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 (exact match preferred)
Class Templates
Basic Class Templates
// Simple class template
template<typename T>
class Container {
T value;
public:
Container(T v) : value(v) {}
T get() const { return value; }
void set(T v) { value = v; }
};
// Usage
Container<int> intContainer(42);
Container<std::string> strContainer("hello");
// Multiple template parameters
template<typename K, typename V>
class KeyValuePair {
K key;
V value;
public:
KeyValuePair(K k, V v) : key(k), value(v) {}
K getKey() const { return key; }
V getValue() const { return value; }
};
KeyValuePair<std::string, int> pair("answer", 42);
Template Member Functions
template<typename T>
class Array {
T* data;
size_t size;
public:
Array(size_t s) : size(s), data(new T[s]) {}
~Array() { delete[] data; }
// Template member function
template<typename Func>
void forEach(Func func) {
for (size_t i = 0; i < size; ++i) {
func(data[i]);
}
}
// Template conversion operator
template<typename U>
operator Array<U>() const {
Array<U> result(size);
for (size_t i = 0; i < size; ++i) {
result.data[i] = static_cast<U>(data[i]);
}
return result;
}
};
// Usage
Array<int> arr(5);
arr.forEach([](int& x) { x *= 2; });
Class Template Specialization
// Primary template
template<typename T>
class Storage {
T data;
public:
Storage(T d) : data(d) {}
T get() const { return data; }
};
// Full specialization for pointers
template<typename T>
class Storage<T*> {
T* data;
public:
Storage(T* d) : data(d) {}
T* get() const { return data; }
T& operator*() { return *data; }
};
// Full specialization for bool (bit optimization)
template<>
class Storage<bool> {
unsigned char data : 1;
public:
Storage(bool d) : data(d) {}
bool get() const { return data; }
};
Partial Template Specialization
// Primary template
template<typename T, typename U>
class Pair {
public:
T first;
U second;
void info() { std::cout << "Generic pair" << std::endl; }
};
// Partial specialization: both types the same
template<typename T>
class Pair<T, T> {
public:
T first;
T second;
void info() { std::cout << "Same type pair" << std::endl; }
};
// Partial specialization: second type is pointer
template<typename T, typename U>
class Pair<T, U*> {
public:
T first;
U* second;
void info() { std::cout << "Second is pointer" << std::endl; }
};
// Usage
Pair<int, double> p1; // Generic
Pair<int, int> p2; // Same type
Pair<int, double*> p3; // Second is pointer
Template Parameters
Type Parameters
// Single type parameter
template<typename T>
class Vector {
T* data;
};
// Multiple type parameters
template<typename T, typename Allocator>
class CustomVector {
T* data;
Allocator alloc;
};
// Default type parameters
Установить cpp-templates-metaprogramming в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
BashReadWriteEditFAQ
Что делает скилл cpp-templates-metaprogramming?
Use when creating generic and type-safe C++ libraries with templates, SFINAE, concepts, and compile-time metaprogramming.
Как установить скилл cpp-templates-metaprogramming?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл cpp-templates-metaprogramming запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
theme-factory
Toolkit for styling artifacts with a theme. These artifacts can be slides, docs, reportings, HTML landing pages, etc. There are 10 pre-set themes with…
от Anthropicaccess
Manage Discord channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
от Anthropicconfigure
Set up the Discord channel — save the bot token and review access policy. Use when the user pastes a Discord bot token, asks to configure Discord, asks "how…
от Anthropicaccess
Manage iMessage channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
от Anthropic