--- id: cpp-functions-lambda title: "C++ Lambda Functions" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["anonymous function", "capture clause", "capture by value vs reference", "C++ λžŒλ‹€ ν•¨μˆ˜"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.87 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["cpp", "programming-language", "w3schools", "functions", "lambda"] raw_sources: ["https://www.w3schools.com/cpp/cpp_functions_lambda.asp"] applied_in: [] github_commit: "" --- # [[CPP Lambda Functions]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) The capture clause's TWO forms β€” `[x]` (by value, a frozen COPY at the moment of creation) versus `[&x]` (by reference, always the LATEST value) β€” produce genuinely different output for the identical-looking code: the same `show()` lambda prints `10` in one version and `20` in the other, purely because `x` was changed to 20 AFTER the lambda was defined β€” meaning the capture-clause choice determines whether a lambda is a snapshot of the past or a live window into the present. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Lambda function** β€” `[capture](parameters) { code };` β€” an anonymous, throwaway function written inline, with no separate name/declaration required. [S1] - **`auto` + lambda** β€” a lambda is typically stored via `auto varName = [...]() {...};` then called like a normal function (`varName();`). [S1] - **Capture by value (`[x]`)** β€” the lambda gets a COPY of `x` frozen at creation time; later changes to the outer `x` don't affect the lambda's copy. [S1] - **Capture by reference (`[&x]`)** β€” the lambda uses the ORIGINAL `x`; it always sees the latest value, even changes made AFTER the lambda was defined. [S1] - **Passing a lambda as an argument** β€” requires `#include ` and a `function` parameter type in the RECEIVING function. [S1] - **Regular function vs. lambda** β€” use a regular function when reused often, needs a clear name, or logic is long; use a lambda for one-off, short, throwaway logic or when passing behavior into another function. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Basic no-parameter lambda: `auto message = []() { cout << "Hello World!\n"; }; message();`. [S1] - Lambda with parameters: `auto add = [](int a, int b) { return a + b; }; cout << add(3, 4); // 7`. [S1] - Passing a lambda to another function: `void myFunction(function func) { func(); func(); } auto message = []() { cout << "Hello World!\n"; }; myFunction(message);` β€” requires `#include `. [S1] - Capture by value freezes the value at creation: `int x = 10; auto show = [x]() { cout << x; }; show(); // 10, even if x changes afterward`. [S1] - Capture by reference sees the LATEST value: `int x = 10; auto show = [&x]() { cout << x; }; x = 20; show(); // 20 β€” reflects the change made after the lambda was created`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **캑처 방식에 따라 λ™μΌν•œ μ½”λ“œκ°€ λ‹€λ₯Έ κ²°κ³Όλ₯Ό 냄**: [x]둜 κ°’ μΊ‘μ²˜ν•˜λ©΄ λžŒλ‹€ 생성 μ‹œμ μ˜ x 값이 κ³ μ •λ˜μ§€λ§Œ, [&x]둜 μ°Έμ‘° μΊ‘μ²˜ν•˜λ©΄ 이후 xκ°€ 바뀐 값을 κ·ΈλŒ€λ‘œ λ°˜μ˜ν•œλ‹€λŠ” 점이 λ™μΌν•œ ꡬ쑰의 두 예제(10 vs 20 좜λ ₯)둜 직접 λŒ€λΉ„λ˜μ–΄ 증λͺ…됨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) 반볡문 μ•ˆμ—μ„œ λ§€ μˆœνšŒλ§ˆλ‹€ λ‹€λ₯Έ 값을 μΊ‘μ²˜ν•˜λŠ” λžŒλ‹€λ₯Ό μ¦‰μ„μ—μ„œ μ •μ˜ν•΄ μ‚¬μš©ν•˜λŠ” νŒ¨ν„΄(`for (int i = 1; i <= 3; i++) { auto show = [i]() {...}; show(); }`)이 μ›λ¬Έμ—μ„œ 직접 μ‹€μ „ ν™œμš© μ‚¬λ‘€λ‘œ μ œμ‹œλ¨. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Capture by value vs. capture by reference producing different output from identical-looking code (C++): ```cpp int x = 10; auto showCopy = [x]() { cout << x; }; // captures a frozen copy auto showRef = [&x]() { cout << x; }; // captures the live variable x = 20; showCopy(); // 10 (unaffected by the later change) showRef(); // 20 (reflects the latest value) ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.87 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C++ Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[CPP Functions Recursion]], [[CPP References]], [[CPP OOP]] - **μ°Έμ‘° λ§₯락:** ν•¨μˆ˜ μ„Ήμ…˜ λ§ˆμ§€λ§‰ β€” 객체지ν–₯ ν”„λ‘œκ·Έλž˜λ° 기초(OOP Basics) μ„Ήμ…˜μœΌλ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C++ Lambda Functions β€” https://www.w3schools.com/cpp/cpp_functions_lambda.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C++ Lambda Functions" page (Astra wiki-curation, P-Reinforce v3.1 format).