简体   繁体   English

为什么C ++ Lambda Capture不需要类型声明?

[英]Why c++ lambda capture needn't type declaration?

A lambda function: Lambda函数:

auto wc = find_if(words.begin(), words.end(),
[sz](const string &a)    //sz does not require type declaration
{
   return a.size() >= sz;
})

is equal to 等于

class SizeComp {
    SizeComp(size_t n): sz(n) { }  // Type required here.
    bool operator()(const string &s) const { return s.size() >= sz; }
private:
    size_t sz; 
};

Why does the lambda capture of sz not require a type declaration? 为什么lambda捕获sz不需要类型声明?

由于捕获的变量是在父作用域中声明的,因此其类型是已知的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM