/cpp-templates
C++ template skill for reading template errors and optimizing compile times. Use when deciphering template error stacks, setting -ftemplate-backtrace-limit, writing concepts and requires-clauses, understanding SFINAE vs concepts, or profiling template instantiation bottlenecks
$ npx -y skills add mohitmishra786/low-level-dev-skills --skill cpp-templates --agent claude-codeHow it fires
How this skill gets triggered: by you, by Claude, or both.
- Fires itselfAuto-invocation. Claude auto-loads it when your prompt matches the work.Auto-invocation is when the right skill fires by itself at the right moment, driven by a FLOW.md router and a hook, instead of you invoking it by name. It is the difference between a skill being installed and a skill actually getting used.Read the full definition →
- You can call itInvoke it directly when you want it.
- Slash command
/cpp-templates
Context preview
The summary Claude sees to decide when to auto-load this skill.
C++ template skill for reading template errors and optimizing compile times. Use when deciphering template error stacks, setting -ftemplate-backtrace-limit, writing concepts and requires-clauses, understanding SFINAE vs concepts, or profiling template instantiation bottlenecks
SKILL.md
cpp-templates.SKILL.mdname: cpp-templates
description: C++ template skill for reading template errors and optimizing compile times. Use when deciphering template error stacks, setting -ftemplate-backtrace-limit, writing concepts and requires-clauses, understanding SFINAE vs concepts, or profiling template instantiation bottlenecks with Templight. Activates on queries about C++ templates, template error messages, concepts, requires expressions, SFINAE, template metaprogramming, or slow template compilation.
C++ Templates
Purpose
Guide agents through reading and fixing template error messages, using concepts as cleaner constraints, understanding SFINAE vs concepts trade-offs, and profiling template instantiation depth and compile times with Templight.
Triggers
- "How do I read this massive C++ template error?"
- "How do I use concepts to constrain a template?"
- "What's the difference between SFINAE and concepts?"
- "My templates make compilation very slow"
- "How do I write a requires-clause?"
- "How do I profile template instantiation times?"
Workflow
1. Reading template error messages
Template errors print full instantiation chains. Strategy: read from the bottom up.
prog.cpp:25:5: error: no matching function for call to 'sort'
std::sort(v.begin(), v.end());
^~~~~~~~~
/usr/include/c++/13/bits/stl_algo.h:4869:5: note: candidate:
template<class _RAIter>
void std::sort(_RAIter, _RAIter)
note: template argument deduction/substitution failed:
prog.cpp:25:5: note: 'MyType' is not a valid type for this template
^~~~~~~~Rules for reading: 1. Find the first error line (top of output) — that's your code 2. Skip all the `note:` lines until you find "required from here" or "in instantiation of" 3. The bottom of the stack shows the type that failed substitution
# Limit backtrace depth to reduce noise
g++ -ftemplate-backtrace-limit=3 prog.cpp
clang -ftemplate-depth=32 prog.cpp # default 1024
# Show simplified errors (GCC 12+)
g++ -fconcepts-diagnostics-depth=3 prog.cpp # for concept failures
2. SFINAE — legacy constraint technique
SFINAE (Substitution Failure Is Not An Error) silently removes overloads that fail substitution:
#include <type_traits>
// Enable function only for arithmetic types
template <typename T,
std::enable_if_t<std::is_arithmetic_v<T>, int> = 0>
T square(T x) { return x * x; }
// SFINAE with return type
template <typename T>
auto to_string(T x) -> std::enable_if_t<std::is_integral_v<T>, std::string> {
return std::to_string(x);
}
// Void-t technique for detecting member existence
template <typename, typename = void>
struct has_size : std::false_type {};
template <typename T>
struct has_size<T, std::void_t<decltype(std::declval<T>().size())>>
: std::true_type {};SFINAE errors are cryptic. Prefer concepts (C++20) for new code.
3. Concepts — modern constraints (C++20)
#include <concepts>
// Define a concept
template <typename T>
concept Arithmetic = std::is_arithmetic_v<T>;
template <typename T>
concept Printable = requires(T x) {
{ std::cout << x } -> std::same_as<std::ostream&>;
};
template <typename T>
concept Container = requires(T c) {
c.begin();
c.end();
c.size();
typename T::value_type;
};
// Apply concept as constraint
template <Arithmetic T>
T square(T x) { return x * x; }
// Abbreviated function template (C++20)
auto square(Arithmetic auto x) { return x * x; }
// requires-clause (more complex conditions)
template <typename T>
requires Arithmetic<T> && (sizeof(T) >= 4)
T big_square(T x) { return x * x; }
// Concept in auto parameter
void print_container(const Container auto& c) {
for (const auto& elem : c) std::cout << elem << ' ';
}4. Requires expressions
// requires { expression; } — checks expression is valid
// requires { expression -> type; } — checks type of expression
template <typename T>
concept HasPush = requires(T c, typename T::value_type v) {
c.push_back(v); // must be valid
{ c.front() } -> std::same_as<typename T::value_type&>; // type check
{ c.size() } -> std::convertible_to<std::size_t>; // convertible
requires std::default_initializable<T>; // nested requirement
};
// Compound requires (all must hold)
template <typename T>
concept Sortable = requires(T a, T b) {
{ a < b } -> std::convertible_to<bool>;
{ a == b } -> std::convertible_to<bool>;
};5. SFINAE vs concepts comparison
| Aspect | SFINAE | Concepts | |--------|--------|---------| | Syntax | Complex, verbose | Clean, readable | | Error messages | Cryptic wall-of-text | Clear constraint failure | | Compile time | Can be slow (many substitutions) | Generally faster | | C++ version | C++11 | C++20 | | Short-circuit | No | Yes (concept subsumption) | | Use in `if constexpr` | Awkward | Natural | | Overload ranking | Manually via priority | Automatic by constraint specificity |
Migration: replace `enable_if` with concept constraints; replace `void_t` helpers with `requires`.
6. Template instantiation profiling with Templight
# Install Templight (Clang-based profiler)
# https://github.com/mikael-s-persson/templight
# Build with Templight tracing
clang++ -Xtemplight -profiler -Xtemplight -memory \
-std=c++17 prog.cpp -o prog
# Convert trace to visualizable format
templight-convert -f callgrind -o prof.out templight.pb
# View with KCachegrind
kcachegrind prof.out
# Find top template instantiation costs (without Templight)
# ClangBuildAnalyzer (easier)
ClangBuildAnalyzer --start /tmp/build
cmake --build build
ClangBuildAnalyzer --stop /tmp/build capture.bin
ClangBuildAnalyzer --analyze capture.bin | head -507. Reducing template compile times
// 1. Explicit instantiation — compile once, use everywhere
// header.h
template <typename T>
T transform(T x);
extern template int transform<int>(int); // sup
Read more
name: cpp-templates description: C++ template skill for reading template errors and optimizing compile times. Use when deciphering template error stacks, setting -ftemplate-backtrace-limit, writing concepts and requires-clauses, understanding SFINAE vs concepts, or profiling template instantiation bottlenecks with Templight. Activates on queries about C++ templates, template error messages, concepts, requires expressions, SFINAE, template metaprogramming, or slow template compilation.
C++ Templates
Purpose
Guide agents through reading and fixing template error messages, using concepts as cleaner constraints, understanding SFINAE vs concepts trade-offs, and profiling template instantiation depth and compile times with Templight.
Triggers
- "How do I read this massive C++ template error?"
- "How do I use concepts to constrain a template?"
- "What's the difference between SFINAE and concepts?"
- "My templates make compilation very slow"
- "How do I write a requires-clause?"
- "How do I profile template instantiation times?"
Workflow
1. Reading template error messages
Template errors print full instantiation chains. Strategy: read from the bottom up.
prog.cpp:25:5: error: no matching function for call to 'sort'
std::sort(v.begin(), v.end());
^~~~~~~~~
/usr/include/c++/13/bits/stl_algo.h:4869:5: note: candidate:
template<class _RAIter>
void std::sort(_RAIter, _RAIter)
note: template argument deduction/substitution failed:
prog.cpp:25:5: note: 'MyType' is not a valid type for this template
^~~~~~~~Rules for reading: 1. Find the first error line (top of output) — that's your code 2. Skip all the `note:` lines until you find "required from here" or "in instantiation of" 3. The bottom of the stack shows the type that failed substitution
# Limit backtrace depth to reduce noise g++ -ftemplate-backtrace-limit=3 prog.cpp clang -ftemplate-depth=32 prog.cpp # default 1024 # Show simplified errors (GCC 12+) g++ -fconcepts-diagnostics-depth=3 prog.cpp # for concept failures
2. SFINAE — legacy constraint technique
SFINAE (Substitution Failure Is Not An Error) silently removes overloads that fail substitution:
#include <type_traits>
// Enable function only for arithmetic types
template <typename T,
std::enable_if_t<std::is_arithmetic_v<T>, int> = 0>
T square(T x) { return x * x; }
// SFINAE with return type
template <typename T>
auto to_string(T x) -> std::enable_if_t<std::is_integral_v<T>, std::string> {
return std::to_string(x);
}
// Void-t technique for detecting member existence
template <typename, typename = void>
struct has_size : std::false_type {};
template <typename T>
struct has_size<T, std::void_t<decltype(std::declval<T>().size())>>
: std::true_type {};SFINAE errors are cryptic. Prefer concepts (C++20) for new code.
3. Concepts — modern constraints (C++20)
#include <concepts>
// Define a concept
template <typename T>
concept Arithmetic = std::is_arithmetic_v<T>;
template <typename T>
concept Printable = requires(T x) {
{ std::cout << x } -> std::same_as<std::ostream&>;
};
template <typename T>
concept Container = requires(T c) {
c.begin();
c.end();
c.size();
typename T::value_type;
};
// Apply concept as constraint
template <Arithmetic T>
T square(T x) { return x * x; }
// Abbreviated function template (C++20)
auto square(Arithmetic auto x) { return x * x; }
// requires-clause (more complex conditions)
template <typename T>
requires Arithmetic<T> && (sizeof(T) >= 4)
T big_square(T x) { return x * x; }
// Concept in auto parameter
void print_container(const Container auto& c) {
for (const auto& elem : c) std::cout << elem << ' ';
}4. Requires expressions
// requires { expression; } — checks expression is valid
// requires { expression -> type; } — checks type of expression
template <typename T>
concept HasPush = requires(T c, typename T::value_type v) {
c.push_back(v); // must be valid
{ c.front() } -> std::same_as<typename T::value_type&>; // type check
{ c.size() } -> std::convertible_to<std::size_t>; // convertible
requires std::default_initializable<T>; // nested requirement
};
// Compound requires (all must hold)
template <typename T>
concept Sortable = requires(T a, T b) {
{ a < b } -> std::convertible_to<bool>;
{ a == b } -> std::convertible_to<bool>;
};5. SFINAE vs concepts comparison
| Aspect | SFINAE | Concepts | |--------|--------|---------| | Syntax | Complex, verbose | Clean, readable | | Error messages | Cryptic wall-of-text | Clear constraint failure | | Compile time | Can be slow (many substitutions) | Generally faster | | C++ version | C++11 | C++20 | | Short-circuit | No | Yes (concept subsumption) | | Use in `if constexpr` | Awkward | Natural | | Overload ranking | Manually via priority | Automatic by constraint specificity |
Migration: replace `enable_if` with concept constraints; replace `void_t` helpers with `requires`.
6. Template instantiation profiling with Templight
# Install Templight (Clang-based profiler)
# https://github.com/mikael-s-persson/templight
# Build with Templight tracing
clang++ -Xtemplight -profiler -Xtemplight -memory \
-std=c++17 prog.cpp -o prog
# Convert trace to visualizable format
templight-convert -f callgrind -o prof.out templight.pb
# View with KCachegrind
kcachegrind prof.out
# Find top template instantiation costs (without Templight)
# ClangBuildAnalyzer (easier)
ClangBuildAnalyzer --start /tmp/build
cmake --build build
ClangBuildAnalyzer --stop /tmp/build capture.bin
ClangBuildAnalyzer --analyze capture.bin | head -507. Reducing template compile times
// 1. Explicit instantiation — compile once, use everywhere // header.h template <typename T> T transform(T x); extern template int transform<int>(int); // sup
A curated suite of AI agent skills for systems and low-level programming — C/C++, Rust, Zig, GPU, bare-metal firmware, Linux kernel/driver development, computer architecture, compiler internals, HPC, and more.
Repo: mohitmishra786/low-level-dev-skills
Other skills on low-level-dev-skills.
- /custom-allocators
Custom allocator skill for memory allocation strategies. Use when implementing pool/slab/arena allocators, tuning jemalloc/mimalloc, writing Rust GlobalAlloc, or benchmarking allocator performance. Activates on queries about jemalloc, mimalloc, tcmalloc, arena allocator,
Open skill - /numa-programming
NUMA programming skill for multi-socket memory locality. Use when detecting NUMA topology, binding processes with numactl, using libnuma API, building NUMA-aware data structures, or measuring remote access penalties. Activates on queries about numactl, libnuma, NUMA topology,
Open skill - /af-xdp
AF_XDP skill for high-performance XDP sockets. Use when creating AF_XDP sockets, configuring UMEM and XSK rings, XDP_REDIRECT programs, copy vs zero-copy mode, or comparing with DPDK. Activates on queries about AF_XDP, xsk_umem, XDP_REDIRECT, libbpf xsk, or zero-copy XDP.
Open skill - /dpdk
DPDK skill for userspace packet I/O. Use when initializing EAL, configuring PMD drivers, using mbuf pools and rte_ring, setting up huge pages, RSS, or testpmd validation. Activates on queries about DPDK, EAL, rte_eth_rx_burst, hugepages, PMD, or testpmd.
Open skill - /io-uring
io_uring skill for Linux async I/O. Use when building high-performance servers with liburing, multi-shot operations, provided buffers, fixed files, zero-copy send, or tokio-uring. Activates on queries about io_uring, SQE/CQE, liburing, IORING_OP_PROVIDE_BUFFERS, or io_uring vs
Open skill - /adc-dac-baremetal
Bare-metal ADC and DAC skill. Use when configuring analog sampling, DMA-driven ADC, calibration, or DAC output on MCUs. Activates on queries about ADC bare-metal, sampling time, DMA ADC, or DAC channel setup.
Open skill

