File tree Expand file tree Collapse file tree 3 files changed +6
-3
lines changed
Expand file tree Collapse file tree 3 files changed +6
-3
lines changed Original file line number Diff line number Diff line change @@ -218,7 +218,8 @@ int main() {
218218 v,
219219 key,
220220 {},
221- [](const X& x) { return x.name; }
221+ // x.nameがコピーされないよう戻り値型を明示的に指定
222+ [](const X& x) -> const std::string& { return x.name; }
222223);
223224 std::cout << "[ラムダ式]" << std::endl;
224225 for (const X& x : result2) {
Original file line number Diff line number Diff line change @@ -140,7 +140,8 @@ int main() {
140140 auto it = std::ranges::find(
141141 v,
142142 std::string("bbb"),
143- [ ] (const Item& x) { return x.name; }
143+ // コピーされないよう戻り値型を明示的に指定
144+ [ ] (const Item& x) -> const std::string& { return x.name; }
144145 );
145146 if (it == v.end()) {
146147 std::cout << "not found" << std::endl;
Original file line number Diff line number Diff line change @@ -267,7 +267,8 @@ int main() {
267267 v,
268268 key,
269269 {},
270- [](const X& x) { return x.name; }
270+ // コピーされないよう戻り値型を明示的に指定
271+ [](const X& x) -> const std::string& { return x.name; }
271272 );
272273 if (it2 != v.end() && it2->name == key) {
273274 std::size_t pos = std::ranges::distance(v.begin(), it2);
You can’t perform that action at this time.
0 commit comments