为什么 c++ lambda 捕获不需要类型声明?

Why c++ lambda capture needn't type declaration?

本文关键字:类型 声明 不需要 c++ lambda 为什么      更新时间:2023-10-16

lambda函数:

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

等于

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; 
};

为什么lambda捕获SZ不需要类型声明?

由于捕获的变量在父范围中声明了,其类型已经已知。