auto closure = []() { cout << "Hello, world!" << endl; }; closure(); // Output: Hello, world!
auto closure1 = []() -> int { return 42; }; int result1 = closure1(); auto closure2 = [](int x) -> string { return to_string(x); }; string result2 = closure2(42); C++에서는 lambda expression을 사용하여 closure를 구현할 수 있습니다. Lambda expression은 함수처럼 사용할 수 있는 익명 함수입니다.
이 함수를 정의하면서 변수에 함수를 할당하거나 다른 함수의 인자로 전달할 수 있습니다. C++에서는 이 lambda expression을 사용하여 closure를 구현합니다.
Closure는 함수와 비슷한 개념으로서, 자신이 정의된 범위 내의 변수와 상태를 캡처하여...
원문 링크 : [C++] Closure