Skip to content

Commit bf8de7a

Browse files
fix: ci
1 parent 63e504a commit bf8de7a

20 files changed

Lines changed: 427 additions & 332 deletions

documents/en/vol4-advanced/vol1-basics-cpp11-14/01-templates-introduction.md

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
---
2-
title: "Templates, From Scratch: A Code Recipe with Placeholders"
3-
description: "Strip templates back to what they really are: a code recipe with placeholders. How they differ from macros and virtual dispatch, and the four kinds of template entities in C++ (functions, classes, variables, aliases)."
42
chapter: 12
5-
order: 1
6-
tags:
7-
- host
8-
- cpp-modern
9-
- intermediate
10-
- 模板
11-
- 泛型
3+
cpp_standard:
4+
- 11
5+
- 14
6+
- 17
7+
- 20
8+
description: 'Strip templates back to what they really are: a code recipe with placeholders.
9+
How they differ from macros and virtual dispatch, and the four kinds of template
10+
entities in C++ (functions, classes, variables, aliases).'
1211
difficulty: intermediate
12+
order: 1
1313
platform: host
14-
cpp_standard: [11, 14, 17, 20]
15-
reading_time_minutes: 18
1614
prerequisites:
17-
- "Volume 1 · Function Templates"
15+
- Volume 1 · Function Templates
16+
reading_time_minutes: 11
1817
related:
19-
- "Function Templates, In Depth: Compilation Model and extern template"
20-
- "Class Templates: Members, Dependent Names, Lazy Instantiation"
18+
- 'Function Templates, In Depth: Compilation Model and extern template'
19+
- 'Class Templates: Members, Dependent Names, Lazy Instantiation'
20+
tags:
21+
- host
22+
- cpp-modern
23+
- intermediate
24+
- 模板
25+
- 泛型
26+
title: 'Templates, From Scratch: A Code Recipe with Placeholders'
2127
---
22-
2328
# Templates, From Scratch: A Code Recipe with Placeholders
2429

2530
We already wrote function templates back in Volume 1. We know that `template <typename T> T max_value(T a, T b)` lets the compiler stamp out a version for `int`, for `double`, for `std::string`. This volume stops at "how to use them" and asks different questions. What is a template, really? How does it pull off "write once, fit any type"? And what does that mechanism cost? Get the under-the-hood details straight, and reading the STL source, reading template-heavy industrial code like Chromium, or writing a library of your own stops feeling intimidating.

documents/en/vol4-advanced/vol1-basics-cpp11-14/02-function-templates-deep.md

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
---
2-
title: "Function Templates, In Depth: Compilation Model and the No-Partial-Specialization Trap"
3-
description: "Re-reading function templates from a library writer's angle: why the inclusion model forces templates into headers, how explicit instantiation and extern template control code bloat, and the classic trap that function templates cannot be partially specialized."
42
chapter: 12
5-
order: 2
6-
tags:
7-
- host
8-
- cpp-modern
9-
- intermediate
10-
- 模板
11-
- 泛型
3+
cpp_standard:
4+
- 11
5+
- 14
6+
- 17
7+
description: 'Re-reading function templates from a library writer''s angle: why the
8+
inclusion model forces templates into headers, how explicit instantiation and extern
9+
template control code bloat, and the classic trap that function templates cannot
10+
be partially specialized.'
1211
difficulty: intermediate
12+
order: 2
1313
platform: host
14-
cpp_standard: [11, 14, 17]
15-
reading_time_minutes: 20
1614
prerequisites:
17-
- "Volume 1 · Function Templates"
18-
- "Templates, From Scratch: A Code Recipe with Placeholders"
15+
- Volume 1 · Function Templates
16+
- 'Templates, From Scratch: A Code Recipe with Placeholders'
17+
reading_time_minutes: 11
1918
related:
20-
- "Class Templates: Members, Dependent Names, Lazy Instantiation"
21-
- "Template Specialization and Partial Specialization: The Art of Pattern Matching"
19+
- 'Class Templates: Members, Dependent Names, Lazy Instantiation'
20+
- 'Template Specialization and Partial Specialization: The Art of Pattern Matching'
21+
tags:
22+
- host
23+
- cpp-modern
24+
- intermediate
25+
- 模板
26+
- 泛型
27+
title: 'Function Templates, In Depth: Compilation Model and the No-Partial-Specialization
28+
Trap'
2229
---
23-
2430
# Function Templates, In Depth: Compilation Model and the No-Partial-Specialization Trap
2531

2632
Volume 1 already covered function templates: the syntax, instantiation, deduction, specialization, and overloading. This piece shifts perspective to "I want to build a library with templates," where three things are unavoidable. Why the compilation model for templates differs from ordinary functions. How explicit instantiation and `extern template` help you control code bloat. And a trap that can leave you staring at the screen for half an hour: function templates cannot be partially specialized. Get these three straight, and reading the source of STL or template-heavy libraries like Eigen starts to make sense, why they organize the code the way they do.

documents/en/vol4-advanced/vol1-basics-cpp11-14/03-class-templates.md

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
---
2-
title: "Class Templates: Members, Dependent Names, and Lazy Instantiation"
3-
description: "Class templates are the foundation of the STL. This piece covers the three differences from function templates that trip people up: members defined inside or outside the class, the lazy-instantiation temper, and why dependent names need typename and this-> for disambiguation."
42
chapter: 12
5-
order: 3
6-
tags:
7-
- host
8-
- cpp-modern
9-
- intermediate
10-
- 模板
11-
- 泛型
3+
cpp_standard:
4+
- 11
5+
- 14
6+
- 17
7+
description: 'Class templates are the foundation of the STL. This piece covers the
8+
three differences from function templates that trip people up: members defined inside
9+
or outside the class, the lazy-instantiation temper, and why dependent names need
10+
typename and this-> for disambiguation.'
1211
difficulty: intermediate
12+
order: 3
1313
platform: host
14-
cpp_standard: [11, 14, 17]
15-
reading_time_minutes: 20
1614
prerequisites:
17-
- "Templates, From Scratch: A Code Recipe with Placeholders"
18-
- "Function Templates, In Depth: Compilation Model and the No-Partial-Specialization Trap"
15+
- 'Templates, From Scratch: A Code Recipe with Placeholders'
16+
- 'Function Templates, In Depth: Compilation Model and the No-Partial-Specialization
17+
Trap'
18+
reading_time_minutes: 10
1919
related:
20-
- "Template Specialization and Partial Specialization: The Art of Pattern Matching"
21-
- "Name Lookup and ADL: How Two-Phase Lookup Works"
20+
- 'Template Specialization and Partial Specialization: The Art of Pattern Matching'
21+
- 'Name Lookup and ADL: How Two-Phase Lookup Works'
22+
tags:
23+
- host
24+
- cpp-modern
25+
- intermediate
26+
- 模板
27+
- 泛型
28+
title: 'Class Templates: Members, Dependent Names, and Lazy Instantiation'
2229
---
23-
2430
# Class Templates: Members, Dependent Names, and Lazy Instantiation
2531

2632
Class templates are the foundation of the STL. `std::vector`, `std::map`, and `std::string` (really `std::basic_string<char>`) are all class templates. Knowing function templates does not mean knowing class templates. There are a few key differences, and they happen to be exactly where newcomers stumble. This piece focuses on three: whether member functions go inside or outside the class, the error-hiding temper that lazy instantiation brings, and why dependent names need `typename` and `this->` for disambiguation. Get these three straight, and writing your own containers or policy classes, or reading STL source, stops snagging on these details.

documents/en/vol4-advanced/vol1-basics-cpp11-14/04-specialization-partial.md

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
---
2-
title: "Template Specialization and Partial Specialization: The Art of Pattern Matching"
3-
description: "Specialization is the most expressive part of templates. Full vs partial specialization, the rule by which the compiler picks the most specialized version, and two classic applications: why std::vector<bool> is a partial specialization, and how the entire type_traits machinery (is_pointer, is_const) is built on partial specialization."
42
chapter: 12
5-
order: 4
6-
tags:
7-
- host
8-
- cpp-modern
9-
- intermediate
10-
- 模板
11-
- 泛型
3+
cpp_standard:
4+
- 11
5+
- 14
6+
- 17
7+
description: 'Specialization is the most expressive part of templates. Full vs partial
8+
specialization, the rule by which the compiler picks the most specialized version,
9+
and two classic applications: why std::vector<bool> is a partial specialization,
10+
and how the entire type_traits machinery (is_pointer, is_const) is built on partial
11+
specialization.'
1212
difficulty: intermediate
13+
order: 4
1314
platform: host
14-
cpp_standard: [11, 14, 17]
15-
reading_time_minutes: 20
1615
prerequisites:
17-
- "Class Templates: Members, Dependent Names, and Lazy Instantiation"
18-
- "Function Templates, In Depth: Compilation Model and the No-Partial-Specialization Trap"
16+
- 'Class Templates: Members, Dependent Names, and Lazy Instantiation'
17+
- 'Function Templates, In Depth: Compilation Model and the No-Partial-Specialization
18+
Trap'
19+
reading_time_minutes: 10
1920
related:
20-
- "Non-Type Template Parameters: From Integers to C++20 Floats and Class Types"
21-
- "Name Lookup and ADL: How Two-Phase Lookup Works"
21+
- 'Non-Type Template Parameters: From Integers to C++20 Floats and Class Types'
22+
- 'Name Lookup and ADL: How Two-Phase Lookup Works'
23+
tags:
24+
- host
25+
- cpp-modern
26+
- intermediate
27+
- 模板
28+
- 泛型
29+
title: 'Template Specialization and Partial Specialization: The Art of Pattern Matching'
2230
---
23-
2431
# Template Specialization and Partial Specialization: The Art of Pattern Matching
2532

2633
The strongest part of templates is not "write one, fit any type." It is **giving some specific type, or some family of types, a separate, different implementation.** This mechanism is called specialization. It comes in two forms: full specialization targets one concrete type, partial specialization targets a family of types matching a pattern. The previous piece noted that function templates cannot be partially specialized; only class templates and variable templates can. This piece takes class-template specialization all the way: full specialization, partial specialization, the priority rule for which version the compiler picks, and two classic cases that push partial specialization to the limit, the standard library's `std::vector<bool>` and the whole of `<type_traits>`.

documents/en/vol4-advanced/vol1-basics-cpp11-14/05-non-type-parameters.md

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
---
2-
title: "Non-Type Template Parameters: From Integers to C++20 Floats and Class Types"
3-
description: "A non-type template parameter parameterizes a value, not a type. The N in array<T, N> is one. What types it accepts, the C++17 auto placeholder, how C++20 opened it up to floating-point and structural class types, and which arguments count as the same instantiation."
42
chapter: 12
5-
order: 5
6-
tags:
7-
- host
8-
- cpp-modern
9-
- intermediate
10-
- 模板
11-
- 泛型
12-
- 编译期计算
3+
cpp_standard:
4+
- 11
5+
- 14
6+
- 17
7+
- 20
8+
description: A non-type template parameter parameterizes a value, not a type. The
9+
N in array<T, N> is one. What types it accepts, the C++17 auto placeholder, how
10+
C++20 opened it up to floating-point and structural class types, and which arguments
11+
count as the same instantiation.
1312
difficulty: intermediate
13+
order: 5
1414
platform: host
15-
cpp_standard: [11, 14, 17, 20]
16-
reading_time_minutes: 18
1715
prerequisites:
18-
- "Template Specialization and Partial Specialization: The Art of Pattern Matching"
19-
- "Class Templates: Members, Dependent Names, and Lazy Instantiation"
16+
- 'Template Specialization and Partial Specialization: The Art of Pattern Matching'
17+
- 'Class Templates: Members, Dependent Names, and Lazy Instantiation'
18+
reading_time_minutes: 9
2019
related:
21-
- "Name Lookup and ADL: How Two-Phase Lookup Works"
22-
- "Template Friends and Barton-Nackman: The Hidden Friends Trick"
20+
- 'Name Lookup and ADL: How Two-Phase Lookup Works'
21+
- 'Template Friends and Barton-Nackman: The Hidden Friends Trick'
22+
tags:
23+
- host
24+
- cpp-modern
25+
- intermediate
26+
- 模板
27+
- 泛型
28+
- 编译期计算
29+
title: 'Non-Type Template Parameters: From Integers to C++20 Floats and Class Types'
2330
---
24-
2531
# Non-Type Template Parameters: From Integers to C++20 Floats and Class Types
2632

2733
The previous pieces parameterized types. `typename T` stands in for a type. A template can also parameterize something else: **a value known at compile time**. The `N` in `std::array<T, N>` and `std::bitset<N>` is exactly this kind of parameter, called a non-type template parameter. This piece covers what types of values it can take, how C++17 `auto` made it more flexible, how C++20 opened it up from "integers and pointers only" to "floating-point and even class types," and which arguments count as the "same" instantiation. The C++20 relaxation is the biggest upgrade non-type parameters have had since they were invented, and it enables fixed_string, compile-time constant objects, and other new tricks.

documents/en/vol4-advanced/vol1-basics-cpp11-14/06-name-lookup-and-adl.md

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
---
2-
title: "Name Lookup and ADL: How Two-Phase Lookup Works"
3-
description: "Name lookup inside templates works nothing like ordinary code. It happens in two phases. This piece covers two-phase lookup, dependent vs non-dependent names, ADL (argument-dependent lookup), and why the typename, this->, and hidden-friends rules from earlier pieces have to exist."
42
chapter: 12
5-
order: 6
6-
tags:
7-
- host
8-
- cpp-modern
9-
- intermediate
10-
- 模板
11-
- 泛型
3+
cpp_standard:
4+
- 11
5+
- 14
6+
- 17
7+
- 20
8+
description: Name lookup inside templates works nothing like ordinary code. It happens
9+
in two phases. This piece covers two-phase lookup, dependent vs non-dependent names,
10+
ADL (argument-dependent lookup), and why the typename, this->, and hidden-friends
11+
rules from earlier pieces have to exist.
1212
difficulty: intermediate
13+
order: 6
1314
platform: host
14-
cpp_standard: [11, 14, 17, 20]
15-
reading_time_minutes: 20
1615
prerequisites:
17-
- "Class Templates: Members, Dependent Names, and Lazy Instantiation"
18-
- "Non-Type Template Parameters: From Integers to C++20 Floats and Class Types"
16+
- 'Class Templates: Members, Dependent Names, and Lazy Instantiation'
17+
- 'Non-Type Template Parameters: From Integers to C++20 Floats and Class Types'
18+
reading_time_minutes: 9
1919
related:
20-
- "Template Friends and Barton-Nackman: The Hidden Friends Trick"
21-
- "Template Specialization and Partial Specialization: The Art of Pattern Matching"
20+
- 'Template Friends and Barton-Nackman: The Hidden Friends Trick'
21+
- 'Template Specialization and Partial Specialization: The Art of Pattern Matching'
22+
tags:
23+
- host
24+
- cpp-modern
25+
- intermediate
26+
- 模板
27+
- 泛型
28+
title: 'Name Lookup and ADL: How Two-Phase Lookup Works'
2229
---
23-
2430
# Name Lookup and ADL: How Two-Phase Lookup Works
2531

2632
In ordinary code, when you use a name, the compiler looks it up at the current position, finds it, and uses it. In templates, that stops working. Name lookup in templates happens in two phases, one at the template definition and one at instantiation. This mechanism is called two-phase lookup, and it is exactly what explains the seemingly strange rules from earlier pieces: why `typename` cannot be dropped, why `this->` is mandatory, and why hidden friends matter. This piece takes two-phase lookup, dependent and non-dependent names, and ADL all the way through. After it, the "inexplicable" errors you hit in templates will all trace back to a root cause you can name.

documents/en/vol4-advanced/vol1-basics-cpp11-14/07-friends-and-barton-nackman.md

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
---
2-
title: "Template Friends and Barton-Nackman: The Hidden Friends Trick"
3-
description: "The previous piece covered ADL. This one covers its most elegant partner: hidden friends and the Barton-Nackman trick. Define operator== as a friend inside a class template, and each instantiation gets an operator dedicated to its own type, without polluting the global overload pool, discoverable by ADL on exact match."
42
chapter: 12
5-
order: 7
6-
tags:
7-
- host
8-
- cpp-modern
9-
- intermediate
10-
- 模板
11-
- 泛型
3+
cpp_standard:
4+
- 11
5+
- 14
6+
- 17
7+
- 20
8+
description: 'The previous piece covered ADL. This one covers its most elegant partner:
9+
hidden friends and the Barton-Nackman trick. Define operator== as a friend inside
10+
a class template, and each instantiation gets an operator dedicated to its own type,
11+
without polluting the global overload pool, discoverable by ADL on exact match.'
1212
difficulty: intermediate
13+
order: 7
1314
platform: host
14-
cpp_standard: [11, 14, 17, 20]
15-
reading_time_minutes: 18
1615
prerequisites:
17-
- "Name Lookup and ADL: How Two-Phase Lookup Works"
18-
- "Class Templates: Members, Dependent Names, and Lazy Instantiation"
16+
- 'Name Lookup and ADL: How Two-Phase Lookup Works'
17+
- 'Class Templates: Members, Dependent Names, and Lazy Instantiation'
18+
reading_time_minutes: 7
1919
related:
20-
- "Alias Templates and using Declarations: Short Names for Types"
21-
- "CRTP: Static Polymorphism with the Curiously Recurring Template Pattern"
20+
- 'Alias Templates and using Declarations: Short Names for Types'
21+
- 'CRTP: Static Polymorphism with the Curiously Recurring Template Pattern'
22+
tags:
23+
- host
24+
- cpp-modern
25+
- intermediate
26+
- 模板
27+
- 泛型
28+
title: 'Template Friends and Barton-Nackman: The Hidden Friends Trick'
2229
---
23-
2430
# Template Friends and Barton-Nackman: The Hidden Friends Trick
2531

2632
The previous piece covered ADL. This one covers its most elegant partner: **hidden friends** and the **Barton-Nackman trick**. The core idea in one sentence: define operators (like `operator==`, `operator<<`) as **friends of a class template, with the definition directly inside the class**. Each instantiation then gets a non-template operator function dedicated to that type. It neither pollutes the global overload pool, and it is discoverable by ADL on exact type match. This is the recommended way to give operators to a custom type in modern C++, and the standard library uses it heavily.

documents/en/vol4-advanced/vol1-basics-cpp11-14/08-alias-and-using.md

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
---
2-
title: "Alias Templates and using Declarations: Short Names for Types"
3-
description: "C++11 alias templates (template<typename T> using X = ...) fix the old problem that typedef cannot be parameterized. The _t alias (from C++14) and the _v variable (from C++17) make type_traits clean to write, and using introduces dependent-base names in template inheritance. This piece covers all three uses."
42
chapter: 12
5-
order: 8
6-
tags:
7-
- host
8-
- cpp-modern
9-
- intermediate
10-
- 模板
11-
- 类型别名
12-
- 泛型
3+
cpp_standard:
4+
- 11
5+
- 14
6+
- 17
7+
description: C++11 alias templates (template<typename T> using X = ...) fix the old
8+
problem that typedef cannot be parameterized. The _t alias (from C++14) and the
9+
_v variable (from C++17) make type_traits clean to write, and using introduces dependent-base
10+
names in template inheritance. This piece covers all three uses.
1311
difficulty: intermediate
12+
order: 8
1413
platform: host
15-
cpp_standard: [11, 14, 17]
16-
reading_time_minutes: 16
1714
prerequisites:
18-
- "Name Lookup and ADL: How Two-Phase Lookup Works"
19-
- "Class Templates: Members, Dependent Names, and Lazy Instantiation"
15+
- 'Name Lookup and ADL: How Two-Phase Lookup Works'
16+
- 'Class Templates: Members, Dependent Names, and Lazy Instantiation'
17+
reading_time_minutes: 7
2018
related:
21-
- "Template Friends and Barton-Nackman: The Hidden Friends Trick"
22-
- "CRTP: Static Polymorphism with the Curiously Recurring Template Pattern"
19+
- 'Template Friends and Barton-Nackman: The Hidden Friends Trick'
20+
- 'CRTP: Static Polymorphism with the Curiously Recurring Template Pattern'
21+
tags:
22+
- host
23+
- cpp-modern
24+
- intermediate
25+
- 模板
26+
- 类型别名
27+
- 泛型
28+
title: 'Alias Templates and using Declarations: Short Names for Types'
2329
---
24-
2530
# Alias Templates and using Declarations: Short Names for Types
2631

2732
C++11 upgraded type aliasing with a new use of `using` and the **alias template**. The old `typedef` can give one type a new name, but it cannot be parameterized. You want a short name for `std::vector<T>`, and `typedef` cannot do it; you have to go through a nested type inside a class template. `using` plus alias templates fix this directly. This piece covers three uses: the alias template itself, the `_t` alias (from C++14) and the `_v` variable (from C++17) used to type_traits, and the role of `using` in introducing base-class names in template inheritance (a follow-up to the `this->` discussion in piece three).

0 commit comments

Comments
 (0)