lambda中使用的静态变量是否在函数调用中保留,其中使用了lambda?或者每个函数调用再次"创建"函数对象?
无用的例子:
#include <iostream>
#include <vector>
#include <algorithm>
using std::cout;
void some_function()
{
std::vector<int> v = {0,1,2,3,4,5};
std::for_each( v.begin(), v.end(),
[](const int &i)
{
static int calls_to_cout = 0;
cout << "cout has been called " << calls_to_cout << " times.\n"
<< "\tCurrent int: " << i << "\n";
++calls_to_cout;
} );
}
int main()
{
some_function();
some_function();
}
Run Code Online (Sandbox Code Playgroud)
这个程序的正确输出是什么?如果lambda捕获局部变量,它是否依赖于事实?(它肯定会改变函数对象的底层实现,因此可能会产生影响)是否允许行为不一致?
我不是在寻找:"我的编译器输出......",这是一个太新的功能,无法信任当前的实现恕我直言.我知道要求标准报价似乎很受欢迎,因为世界发现这样的事情存在,但我仍然想要一个不错的来源.