Lambda Expressions

Lambda Expressions

本文关键字:Expressions Lambda      更新时间:2023-10-16

ISO草案n3290第5.1.2节第19点:

与关联的闭包类型lambda表达式已删除(8.4.3)默认构造函数和已删除副本分配运算符。它具有隐式声明的副本构造函数(12.8),并且可能具有隐式声明的move构造函数(12.8).[注:复制/移动构造函数是在中隐式定义的与任何其他隐含方式相同声明的复制/移动构造函数将可以隐式定义--尾注]

谁能请。。。。讲一些例子来理解这一点?

是否有任何机会/方法来检查闭包对象(类型)?

与lambda表达式关联的闭包类型有一个已删除的(8.4.3)默认构造函数

int main() {
    auto closure = [](){};
    typedef decltype(closure) ClosureType;
    ClosureType closure2;   // <-- not allowed
    return 0;
}

以及一个已删除的拷贝分配运算符。它有一个隐式声明的复制构造函数(12.8),也可能有一个隐含声明的移动构造函数(12.8)

#include <utility>
int main() {
    auto closure = [](){};
    typedef decltype(closure) ClosureType;
    ClosureType closure2 = closure;   // <-- copy constructor
    ClosureType closure3 = std::move(closure);  // <-- move constructor
    closure2 = closure3;              // <-- copy assignment (not allowed)
    return 0;
}
struct LambdaExample{
  // deleted operations = not allowed
  LambdaExample() = delete;
  LambdaExample& operator=(LambdaExample const&) = delete;
  // generated by the compiler:
  LambdaExample(LambdaExample const& other);
  LambdaExample(LambdaExample&& other);
  // anything else a lambda needs
};

对于第二个问题,如果你的意思是可以研究实现,那么不可能。它是由编译器动态创建的。如果你想得到lambda的类型,请确定:

auto l = [](){};
typedef decltype(l) closure_type;