You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs(cpp14): add STL real-world case and d2x setup block to 00-generic-lambdas
- add transparent functor real-world case (std::greater<>) cited from
vendored msvc-stl/stl/inc/functional
- add collapsible d2x setup block in exercise section
- sync en book chapter
Copy file name to clipboardExpand all lines: book/en/src/cpp14/00-generic-lambdas.md
+66-3Lines changed: 66 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -117,7 +117,51 @@ add5(10); // 15
117
117
add5(3.14); // 8.14
118
118
```
119
119
120
-
## II. Notes
120
+
## II. Real-World Case — Transparent Functors and Generic Lambdas in the STL
121
+
122
+
> C++14 introduced transparent operator functors (e.g. `std::less<>` / `std::greater<>`) alongside generic lambdas — both motivated by the same goal: eliminating the need to hard-code concrete types. The examples below cite the vendored [MSVC STL](https://github.com/mcpp-community/d2mcpp/tree/main/msvc-stl) (source: [`msvc-stl/stl/inc/functional`](https://github.com/mcpp-community/d2mcpp/blob/main/msvc-stl/stl/inc/functional#L3420-L3429)); `_EXPORT_STD` / `requires` are internal macros and concepts and can be ignored while reading
123
+
124
+
### std::greater Transparent Specialization — A Comparator That "Sees" Any Type
125
+
126
+
In C++11, `std::greater<T>` locked in the template parameter `T` — `std::greater<int>` could only compare `int`. C++14 added `std::greater<>` (equivalent to `std::greater<void>`), where `operator()` is itself a template accepting any comparable types:
using is_transparent = int; // signals to STL algorithms: this comparator
140
+
// supports heterogeneous types
141
+
};
142
+
```
143
+
144
+
The `operator()` is a template member function — exactly the same underlying mechanism as a generic lambda. The `is_transparent` tag tells algorithms like `std::sort` and `std::set`: "this comparator can compare values of different types"
145
+
146
+
### Transparent Comparators and Generic Lambdas — Two Sides of the Same Coin
147
+
148
+
Transparent functors and generic lambdas are interchangeable for the same use case:
149
+
150
+
```cpp
151
+
std::vector<int> v = {5, 1, 4, 2, 8};
152
+
153
+
// option 1: C++14 transparent comparator
154
+
std::sort(v.begin(), v.end(), std::greater<>());
155
+
156
+
// option 2: C++14 generic lambda
157
+
std::sort(v.begin(), v.end(), [](auto a, auto b) { return a > b; });
158
+
```
159
+
160
+
Both eliminate the C++11 redundancy of writing separate comparators for `int`, `double`, `string`, etc.
161
+
162
+
> Summary: C++14's transparent functors and generic lambdas are two expressions of the same idea — "parameterize the argument types". Transparent functors apply it to callable objects; generic lambdas apply it to lambdas. Understanding how the `is_transparent` tag connects both to STL algorithms shows why the standard library introduced these two features together in C++14
163
+
164
+
## III. Notes
121
165
122
166
### A Generic Lambda Is a Class with a Templated operator()
123
167
@@ -145,7 +189,7 @@ This pattern is common with generic lambdas and is a typical use case for `declt
145
189
146
190
The parameter count is still fixed — `[](auto a, auto b)` accepts exactly two arguments. For variadic parameters, you still need variadic templates (C++20 later added support for `...` parameter packs in lambdas)
147
191
148
-
## III. Exercise Code
192
+
## IV. Exercise Code
149
193
150
194
### Exercise Code Topics
151
195
@@ -154,11 +198,30 @@ The parameter count is still fixed — `[](auto a, auto b)` accepts exactly two
154
198
155
199
### Exercise Auto-Checker Command
156
200
201
+
<details>
202
+
<summary>Don't have d2x? Click for setup</summary>
0 commit comments