본문으로 건너뛰기
Previous
Next
C++ Lambda Capture Error — Complete Guide

C++ Lambda Capture Error — Complete Guide

C++ Lambda Capture Error — Complete Guide

이 글의 핵심

C++ lambda capture error C++, "dangling, reference", Introduction: "Lambda stored but crashes" explained in detail with practical examples.

Introduction: “Lambda Stored but Crashes"

"Reference-captured variable already destroyed”

C++11’s lambda allows writing anonymous functions concisely, but incorrect capture (how lambda uses external variables) causes dangling references or unexpected behavior.

Below is an implementation example using C++. Understand the role of each part while examining the code.

// ❌ Dangling reference
std::function<int()> createLambda() {
    int x = 42;
    return [&x]() { return x; };  // Capture x by reference
}  // x destroyed

int main() {
    auto lambda = createLambda();
    std::cout << lambda() << '\n';  // ❌ Accessing destroyed variable → crash
}

What This Guide Covers:

  • Value capture vs reference capture
  • Preventing dangling references
  • this capture
  • Init capture (C++14)
  • 10 common lambda errors

1. Lambda Capture Basics

Capture Methods

Here is detailed implementation code using C++. Understand the role of each part while examining the code.

int x = 10;
int y = 20;

// [=]: Capture all variables by value
auto lambda1 = [=]() { return x + y; };

// [&]: Capture all variables by reference
auto lambda2 = [&]() { x = 30; return x + y; };

// [x]: Capture only x by value
auto lambda3 = [x]() { return x * 2; };

// [&x]: Capture only x by reference
auto lambda4 = [&x]() { x = 30; };

// [x, &y]: x by value, y by reference
auto lambda5 = [x, &y]() { return x + y; };

// [=, &y]: Default value, only y by reference
auto lambda6 = [=, &y]() { y = 30; return x + y; };

mutable Lambda

Here is detailed implementation code using C++. Understand the role of each part while examining the code.

int x = 10;

// ❌ Value capture cannot modify
auto lambda1 = [x]() {
    // x = 20;  // Compile error: cannot assign to a variable captured by copy
};

// ✅ Can modify with mutable
auto lambda2 = [x]() mutable {
    x = 20;  // OK (modifies internal copy)
    return x;
};

std::cout << lambda2() << '\n';  // 20
std::cout << x << '\n';  // 10 (original unchanged)

2. Value Capture vs Reference Capture

Value Capture [=]

Below is an implementation example using C++. Try running the code directly to check its operation.

int x = 10;

auto lambda = [=]() {  // Copy x
    return x * 2;
};

x = 20;  // Change original
std::cout << lambda() << '\n';  // 20 (value at capture time: 10)

Characteristics:

  • Copies value at capture time
  • No effect from original changes
  • Safe (no dangling references)

Reference Capture [&]

Below is an implementation example using C++. Try running the code directly to check its operation.

int x = 10;

auto lambda = [&]() {  // Reference x
    return x * 2;
};

x = 20;  // Change original
std::cout << lambda() << '\n';  // 40 (current value: 20)

Characteristics:

  • References original
  • Original changes immediately reflected
  • Dangerous (dangling if original destroyed)

Summary

Key Points

  1. Value capture [=]: Safe, copies value
  2. Reference capture [&]: Dangerous, can dangle
  3. mutable: Allows modifying captured values
  4. [this]: Captures this pointer
  5. [*this]: Copies entire object (C++17)

When to Use

Use value capture when:

  • Lambda outlives captured variables
  • Don’t need to modify original
  • Want safety

Use reference capture when:

  • Need to modify original
  • Large objects (performance)
  • Lambda lifetime is short

Don’t use:

  • Reference capture when lambda outlives variable
  • Capturing temporary objects

Best Practices

  • ✅ Default to value capture [=]
  • ✅ Use reference capture [&] carefully
  • ✅ Use shared_ptr for lifetime management
  • ❌ Don’t return lambda with reference capture
  • ❌ Don’t ignore dangling references

Master lambda capture for safer C++ code! 🚀


자주 묻는 질문 (FAQ)

Q. 이 내용을 실무에서 언제 쓰나요?

A. Everything about C++ Lambda Capture Error : from basic concepts to practical applications. Master key content quickly wi… 실무에서는 위 본문의 예제와 선택 가이드를 참고해 적용하면 됩니다.

Q. 선행으로 읽으면 좋은 글은?

A. 각 글 하단의 이전 글 또는 관련 글 링크를 따라가면 순서대로 배울 수 있습니다. C++ 시리즈 목차에서 전체 흐름을 확인할 수 있습니다.

Q. 더 깊이 공부하려면?

A. cppreference와 해당 라이브러리 공식 문서를 참고하세요. 글 말미의 참고 자료 링크도 활용하면 좋습니다.


이 글에서 다루는 키워드 (관련 검색어)

C++, lambda, capture, error-resolution, closure, function-object 등으로 검색하시면 이 글이 도움이 됩니다.