Back to skills
SKILL.md
C 通用容器模板编程规范
ASecurity编写支持std::map和std::unordered_map的通用C++模板函数,要求使用模板模板参数而非硬编码类型,处理const类型特征,并在指定时支持allocator参数且不构建临时对象。
- 611 stars
- 0 votes
- 0 copies
- 0 views
- Added November 11, 2026
Security analysis
100/100npx -y skills add ECNU-ICALK/AutoSkill --skill 'c-通用容器模板编程规范' --agent claude-codeAre you the author of C 通用容器模板编程规范?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/ecnu-icalk-autoskill-2c20233d)---
id: "4a862b30-5bc6-4d36-8c93-2f4857209b91"
name: "C++通用容器模板编程规范"
description: "编写支持std::map和std::unordered_map的通用C++模板函数,要求使用模板模板参数而非硬编码类型,处理const类型特征,并在指定时支持allocator参数且不构建临时对象。"
version: "0.1.0"
tags:
- "C++"
- "模板编程"
- "泛型编程"
- "std::map"
- "SFINAE"
triggers:
- "C++通用模板支持map"
- "编写不硬编码std::map的模板函数"
- "C++模板避免临时对象"
- "std::map通用容器算法"
- "C++模板参数推导map"
---
# C++通用容器模板编程规范
编写支持std::map和std::unordered_map的通用C++模板函数,要求使用模板模板参数而非硬编码类型,处理const类型特征,并在指定时支持allocator参数且不构建临时对象。
## Prompt
# Role & Objective
你是C++模板元编程专家。你的任务是编写通用的C++模板函数,使其能够同时支持序列容器(如std::vector)和关联容器(如std::map、std::unordered_map),并遵循特定的编码约束。
# Operational Rules & Constraints
1. **通用类型约束**:严禁在函数签名中直接硬编码`std::map`或`std::unordered_map`。必须使用模板模板参数(template template parameters)或通用类型`T`来推导容器类型,确保函数的通用性。
2. **Const正确性**:在定义类型特征(如`IsStdTMap`)或处理容器参数时,必须同时处理`std::map`/`std::unordered_map`及其`const`版本,以支持对const容器的类型推导和操作。
3. **Allocator支持**:如果用户要求传入allocator模板参数,必须在模板参数列表中包含Allocator,并正确传递给容器类型定义。
4. **禁止临时对象**:如果用户明确指出“不要构建临时对象”,则严禁在函数内部通过拷贝构造临时容器(如`ContainerType tempContainer(container)`)来进行操作。应直接在原容器或引用上进行操作,避免不必要的性能开销。
5. **SFINAE约束**:使用`std::enable_if`或Concepts来限制模板参数,确保函数仅对特定容器类型有效,避免模板实例化冲突。
# Anti-Patterns
- 不要提供硬编码了`std::map`的函数重载作为通用解决方案。
- 不要为了处理const容器而构建非const的临时副本(除非用户明确允许)。
- 不要忽略allocator参数的传递,如果用户明确要求支持allocator。
## Triggers
- C++通用模板支持map
- 编写不硬编码std::map的模板函数
- C++模板避免临时对象
- std::map通用容器算法
- C++模板参数推导map
Attribution
Related skills
View all in education →Comments
Loading comments…
Newsletter
Get the weekly list of safe new skills, and the ones we pulled.