e9cbf23ab5
이전 재구성 작업에서 Dev/ 폴더가 누락되었던 것을 반영. - Dev/Topic_Programming(중첩 폴더, 78개)은 Dev 자체 최상위 폴더들 (Architecture/Conventions/Engineering_Intelligence 등)과 완전 중복이라 제거. - Dev 최상위 엔지니어링 지식 폴더(Architecture/Conventions/Engineering_Intelligence/ Failure_Library/Generalized_Principles/Language/Pattern_Catalog/Platform_Guides/ Subsystems, 77개)는 이미 Topic_Programming/Topic_Programming에 더 최신 버전이 존재해 중복 제거(고유 콘텐츠 1개는 예외 처리하여 이동 보존). - Dev의 W3Schools 언어 튜토리얼 폴더(Topic_C/CPP/CSS/CSharp/HOWTO/HTML/Java/ JavaScript/PHP/Python/SQL/W3CSS, 1201개)는 전부 Topic_Programming 하위로 이동. - 에이전트 운영 상태(.astra/docs)는 그대로 유지, 콘텐츠 폴더만 정리. - Topic_Programming 최종 문서 수: 2784 → 3985.
4.8 KiB
4.8 KiB
id, title, category, status, verification_status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, created_at, updated_at, review_reason, merge_history, tags, raw_sources, applied_in, github_commit
| id | title | category | status | verification_status | canonical_id | aliases | duplicate_of | source_trust_level | confidence_score | created_at | updated_at | review_reason | merge_history | tags | raw_sources | applied_in | github_commit | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| cpp-functions-lambda | C++ Lambda Functions | Programming_Language | draft | conceptual |
|
B | 0.87 | 2026-07-04 | 2026-07-04 |
|
|
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 viaauto varName = [...]() {...};then called like a normal function (varName();). [S1]- Capture by value (
[x]) — the lambda gets a COPY ofxfrozen at creation time; later changes to the outerxdon't affect the lambda's copy. [S1] - Capture by reference (
[&x]) — the lambda uses the ORIGINALx; it always sees the latest value, even changes made AFTER the lambda was defined. [S1] - Passing a lambda as an argument — requires
#include <functional>and afunction<returnType(paramTypes)>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<void()> func) { func(); func(); } auto message = []() { cout << "Hello World!\n"; }; myFunction(message);— requires#include <functional>. [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++):
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).