Token导航 LogoToken导航TokenDH.com
研究检索需要联网github未标认证来源可访问许可证需确认审计通过

cpp-templates.cpp 模板

Agent Skill

cpp-templates 用于查找、检索和筛选相关信息,适合在 Codex、Claude、Cursor、Gemini CLI 中需要根据关键词、任务场景或来源线索快速定位候选结果时使用。可结合来源仓库、安装命令和原始 README 继续核验具体用法。安装前建议确认权限范围、维护状态,以及是否会触发联网、命令执行或文件读写。

总安装

1,905

周安装

81

GitHub Stars

80

下载量

667
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:cpp-templates(.cpp 模板)
来源仓库:https://github.com/mohitmishra786/low-level-dev-skills
仓库路径:skills/cpp-templates
安装命令:
npx skills add https://github.com/mohitmishra786/low-level-dev-skills --skill cpp-templates
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/mohitmishra786/low-level-dev-skills --skill cpp-templates

简介

cpp-templates 指导阅读和修复 C++ 模板错误,使用 concepts 简化约束表达。

  • 适用于泛型编程和库开发,支持 SFINAE 与 concepts 的对比选择。
  • 提供 Templight 工具分析模板实例化耗时,优化编译性能。
  • 建议优先使用 concepts 而非 SFINAE,提升错误信息可读性和代码安全性。
  • 适用宿主包括 Codex、Claude、Cursor、Gemini CLI,接入前应确认版本、权限和运行环境要求。

SKILL.md

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

AspectSFINAEConcepts
SyntaxComplex, verboseClean, readable
Error messagesCryptic wall-of-textClear constraint failure
Compile timeCan be slow (many substitutions)Generally faster
C++ versionC++11C++20
Short-circuitNoYes (concept subsumption)
Use in if constexprAwkwardNatural
Overload rankingManually via priorityAutomatic 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 -50

7. 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);    // suppress instantiation

// impl.cpp
#include "header.h"
template int transform<int>(int);           // instantiate here only

// 2. Prefer function templates over class templates when possible
// (functions instantiate lazily; class templates instantiate eagerly)

// 3. Use concepts to short-circuit failed substitutions
// (concept check is faster than full substitution failure)

// 4. Split heavy template headers from lightweight ones
// - Put type definitions in forward_decls.h
// - Put template implementations in impl.h (include only where needed)

// 5. Use if constexpr instead of specialization
template <typename T>
void process(T x) {
    if constexpr (std::is_integral_v<T>) {
        handle_int(x);
    } else {
        handle_other(x);
    }
}

Related skills

  • Use skills/build-systems/build-acceleration for ccache and PCH to reduce overall compile time
  • Use skills/compilers/clang for Clang-specific diagnostics and concept error output
  • Use skills/low-level-programming/cpp-coroutines for another advanced C++20 feature

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Claude

31.86%
按下载量换算213

Codex

30.94%
按下载量换算206

Cursor

19.85%
按下载量换算132

Gemini CLI

8.48%
按下载量换算57

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

需要联网

该 Skill 可能需要联网访问来源站点、仓库或外部 API;具体网络访问范围需要结合源码和 README 复核。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills