Returning a lambda capturing a local variable

2020-03-03 06:21发布

Today I encountered a very unintuitive behavior (for me, at least) in C++11 lambdas. The code in question is the following:

#include <stdio.h>

auto sum(int x) {
    return [&x](int y) {
        return x + y;
    };
}

int main() {
    int a = sum(2)(3);
    printf("%d\n",a);
}

Instead of printing 5, this prints gibberish. Actually, at least in my version of GCC, if I turn on the -O2 optimization flag, it actually prints 5. Since the output depends on the optimization level of the compiler, it is undefined behavior. After a while, I think I understood what is happening.

When the function sum is called, a stack variable corresponding to the argument x is set to 2, then the function sum returns, and this stack variable might be overwritten by anything that the compiler needs to put there to execute following code, and by the time the lambda eventually gets executed, the place where x was no longer holds 2, and the program adds 3 to an arbitrary integer.

Is there any elegant way to do currying in C++ guaranteeing that the variable gets captured correctly?

1条回答
萌系小妹纸
2楼-- · 2020-03-03 06:57

int x has a limited lifetime. References to automatic storage variables (what you call "the stack") are only valid over the variable's lifetime. In this case, only until the end of the stack frame (the scope) where the variable exists, or the function for function arguments.

[&] captures any mentioned ("local") variable by reference, except this (which is captured by value if used or implicitly used). [=] captures any mentioned variable by value. [x] would capture x explicitly, and [&x] by reference explicitly. In C++17, [*this] also works.

There is also [x=std::move(x)], or [blah=expression].

In general, if the lambda will outlive the current scope don't use [&]: be explicit about what you capture.

查看更多
登录 后发表回答