b*******s 发帖数: 5216 | 1 http://en.wikipedia.org/wiki/Anonymous_function#C.2B.2B
C++ section
给了两个例子
[]() { ++global_x; } // no parameters, just accessing a global variable
[] //no variables defined. Attempting to use any external variables in the
lambda is an error.
是不是矛盾的啊? |
h***o 发帖数: 30 | 2 only automatic variables can be captured. Global variables can be used
without capturing it.
【在 b*******s 的大作中提到】 : http://en.wikipedia.org/wiki/Anonymous_function#C.2B.2B : C++ section : 给了两个例子 : []() { ++global_x; } // no parameters, just accessing a global variable : [] //no variables defined. Attempting to use any external variables in the : lambda is an error. : 是不是矛盾的啊?
|
b*******s 发帖数: 5216 | 3 多谢多谢
【在 h***o 的大作中提到】 : only automatic variables can be captured. Global variables can be used : without capturing it.
|
i**h 发帖数: 424 | 4 Scope of a global variable is, well, global.
C/C++ does not support nested function, so an auto variable can only be
accessed by the function where it's declared. By capturing, you allow
another function (lambda) to access an auto variable declared in the host
function. The reality is:
[&var]() { f(var); } can be translated into a functor like below:
class lambda
{
public:
lambda(type & var) : m_var(var) {}
void operator() {
f(m_var);
}
private:
type & m_var;
}; |
b*******s 发帖数: 5216 | 5 非常清楚,谢谢
【在 i**h 的大作中提到】 : Scope of a global variable is, well, global. : C/C++ does not support nested function, so an auto variable can only be : accessed by the function where it's declared. By capturing, you allow : another function (lambda) to access an auto variable declared in the host : function. The reality is: : [&var]() { f(var); } can be translated into a functor like below: : class lambda : { : public: : lambda(type & var) : m_var(var) {}
|
b*******s 发帖数: 5216 | 6 非常清楚,谢谢
【在 i**h 的大作中提到】 : Scope of a global variable is, well, global. : C/C++ does not support nested function, so an auto variable can only be : accessed by the function where it's declared. By capturing, you allow : another function (lambda) to access an auto variable declared in the host : function. The reality is: : [&var]() { f(var); } can be translated into a functor like below: : class lambda : { : public: : lambda(type & var) : m_var(var) {}
|