--- id: cpp-templates title: "C++ Templates" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["template keyword", "typename T", "generic programming", "C++ ν…œν”Œλ¦Ώ"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.88 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["cpp", "programming-language", "w3schools", "templates", "generics"] raw_sources: ["https://www.w3schools.com/cpp/cpp_templates.asp"] applied_in: [] github_commit: "" --- # [[CPP Templates]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Templates solve the EXACT problem Function Overloading solved for a FIXED set of types, but generalize it to work with ANY type without writing a separate version for each β€” `template T add(T a, T b)` handles `int`, `double`, or any other type with ONE definition, whereas overloading (from the Functions section) required a separate `plusFunc(int...)` and `plusFunc(double...)` pair, meaning templates are the more powerful, general-purpose evolution of what overloading only approximated for specific type combinations. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Function template** β€” `template return_type function_name(T parameter) { ... }`; `T` is a placeholder for ANY data type, resolved at each CALL site. [S1] - **Explicit type specification** β€” `add(5, 3)` or `add(2.5, 1.5)` tells the compiler which concrete type `T` should be for that specific call. [S1] - **Class template** β€” `template class ClassName { ... };` lets an entire class (members, methods) work generically with any type, instantiated per-type (`Box`, `Box`). [S1] - **Multiple template parameters** β€” `template class Pair { ... };` handles TWO independently-typed values in one generic class (`Pair`, `Pair`). [S1] - **File placement constraint** β€” templates must be defined in the SAME file where they're used (typically the header/`.h` file), unlike ordinary functions which can be split across declaration/definition files. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - A generic add function working for both int and double: `template T add(T a, T b) { return a + b; } cout << add(5, 3); cout << add(2.5, 1.5);`. [S1] - A generic class storing any single value type: `template class Box { public: T value; Box(T v) { value = v; } void show() { cout << "Value: " << value << "\n"; } }; Box intBox(50); Box strBox("Hello");`. [S1] - A generic class handling two independently-typed values: `template class Pair { public: T1 first; T2 second; Pair(T1 a, T2 b) { first = a; second = b; } void display() { cout << "First: " << first << ", Second: " << second << "\n"; } }; Pair person("John", 30); Pair score(51, 9.5);`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **ν…œν”Œλ¦Ώκ³Ό μ˜€λ²„λ‘œλ”©μ€ 같은 λͺ©ν‘œλ₯Ό λ‹€λ₯Έ λ°©μ‹μœΌλ‘œ ν•΄κ²°**: μ˜€λ²„λ‘œλ”©μ€ 각 νƒ€μž… μ‘°ν•©λ§ˆλ‹€ 별도 ν•¨μˆ˜ μ •μ˜κ°€ ν•„μš”ν–ˆμ§€λ§Œ, ν…œν”Œλ¦Ώμ€ ν•˜λ‚˜μ˜ μ •μ˜λ‘œ λͺ¨λ“  νƒ€μž…μ— λŒ€μ‘ν•œλ‹€λŠ” μ μ—μ„œ μ˜€λ²„λ‘œλ”©λ³΄λ‹€ 더 μΌλ°˜ν™”λœ ν•΄λ²•μž„μ΄ 확인됨. [S1] - **ν…œν”Œλ¦Ώμ€ 같은 νŒŒμΌμ— μ •μ˜λ˜μ–΄μ•Ό 함**: 일반 ν•¨μˆ˜μ™€ 달리 ν…œν”Œλ¦Ώμ€ μ„ μ–Έκ³Ό μ •μ˜λ₯Ό 뢄리할 수 μ—†κ³  보톡 헀더(.h) νŒŒμΌμ— ν•¨κ»˜ 두어야 ν•œλ‹€λŠ” μ œμ•½μ΄ λͺ…μ‹œλ¨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) 이름/λ‚˜μ΄(λ¬Έμžμ—΄+μ •μˆ˜), ID/점수(μ •μˆ˜+μ‹€μˆ˜)처럼 μ„œλ‘œ λ‹€λ₯Έ 두 νƒ€μž… 쑰합을 ν•˜λ‚˜μ˜ Pair ν…œν”Œλ¦Ώ 클래슀둜 μ²˜λ¦¬ν•˜λŠ” μ˜ˆμ œκ°€ μ›λ¬Έμ—μ„œ 직접 μ‹€μ „ ν™œμš© μ‚¬λ‘€λ‘œ μ œμ‹œλ¨. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) A generic class template handling two independently-typed values (C++): ```cpp template class Pair { public: T1 first; T2 second; Pair(T1 a, T2 b) { first = a; second = b; } void display() { cout << "First: " << first << ", Second: " << second << "\n"; } }; int main() { Pair person("John", 30); Pair score(51, 9.5); person.display(); score.display(); return 0; } ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.88 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C++ Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[CPP Friend Function]], [[CPP Function Overloading]], [[CPP Namespaces]], [[CPP Vectors]] - **μ°Έμ‘° λ§₯락:** λ‹€ν˜•μ„± 및 κ³ κΈ‰ OOP μ„Ήμ…˜ λ§ˆμ§€λ§‰ β€” λ„€μž„μŠ€νŽ˜μ΄μŠ€(Namespaces) μ„Ήμ…˜μœΌλ‘œ 이어짐, STL μ»¨ν…Œμ΄λ„ˆ(Vectors λ“±)μ—μ„œ ν…œν”Œλ¦Ώμ΄ μ‹€μ œλ‘œ μ“°μ΄λŠ” 방식과 직접 μ—°κ²°. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C++ Templates β€” https://www.w3schools.com/cpp/cpp_templates.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C++ Templates" page (Astra wiki-curation, P-Reinforce v3.1 format).