了解 Lambda 闭包类型如何删除默认构造函数
Understanding how Lambda closure type has deleted default constructor
来自 5.1.2
[19] The closure type associated with a lambda-expression has a deleted (8.4.3) default constructor and a deleted
copy assignment operator. It has an implicitly-declared copy constructor (12.8) and may have an implicitlydeclared
move constructor (12.8). [ Note: The copy/move constructor is implicitly defined in the same way
as any other implicitly declared copy/move constructor would be implicitly defined. —end note ]
我正在通读 C++ Primer 14.8.1,其中解释了编译器将 lambda 表达式翻译成未命名 class 的未命名对象。如果删除默认构造函数,我如何定义不包含 lambda 捕获的 lambda 函数对象?
auto g = [](){};
这在概念上与...不同吗
class lambdaClass{
public:
lambdaClass() = delete;
lambdaClass& operator=(const lambdaClass&) = delete;
void operator()(){ }
//copy/move constructor and destructor implicitly defined
};
auto g = lambdaClass(); //would be an error since default is deleted.
如果有一个捕获,那么将定义一个构造函数 other 而不是默认构造函数,并且可以初始化这样的对象(只要传递了一个参数) .但是如果没有capture,删除了默认的构造函数,那么lambda class object 是可以创建的,在概念上似乎并不一致。
编辑:嗯,也许 lambda class 根据其 lambda 捕获创建构造函数的概念是没有根据的,尽管这是 C++ Primer 中描述的方式(我在标准中找不到它的引用) ,因为即使我希望它在概念上有效,以下代码也不起作用:
int sz = 2;
auto a = [sz](){ return sz;};
decltype(a) b(10); //compiler error
decltype(a) b = a; //all good though
closure到lambda的关系类似于object到class.
C++11 标准 说 closure! 类型没有默认构造函数,这是正确的,因为它没有不要说它没有构造函数。
lambda 用于创建闭包。但是您引用的段落将更改 C++14.
ClosureType() = delete; // (until C++14)
ClosureType(const ClosureType& ) = default; // (since C++14)
ClosureType(ClosureType&& ) = default; // (since C++14)
Closure types are not DefaultConstructible. Closure types have a deleted (until C++14)
no (since C++14)
default constructor. The copy constructor and the move constructor are implicitly-declared (until C++14)
declared as defaulted (since C++14)
and may be implicitly-defined according to the usual rules for copy constructors and move constructors.
因为赋值右边是一个临时对象(右值),所以'g'被移动赋值
来自 5.1.2
[19] The closure type associated with a lambda-expression has a deleted (8.4.3) default constructor and a deleted copy assignment operator. It has an implicitly-declared copy constructor (12.8) and may have an implicitlydeclared move constructor (12.8). [ Note: The copy/move constructor is implicitly defined in the same way as any other implicitly declared copy/move constructor would be implicitly defined. —end note ]
我正在通读 C++ Primer 14.8.1,其中解释了编译器将 lambda 表达式翻译成未命名 class 的未命名对象。如果删除默认构造函数,我如何定义不包含 lambda 捕获的 lambda 函数对象?
auto g = [](){};
这在概念上与...不同吗
class lambdaClass{
public:
lambdaClass() = delete;
lambdaClass& operator=(const lambdaClass&) = delete;
void operator()(){ }
//copy/move constructor and destructor implicitly defined
};
auto g = lambdaClass(); //would be an error since default is deleted.
如果有一个捕获,那么将定义一个构造函数 other 而不是默认构造函数,并且可以初始化这样的对象(只要传递了一个参数) .但是如果没有capture,删除了默认的构造函数,那么lambda class object 是可以创建的,在概念上似乎并不一致。
编辑:嗯,也许 lambda class 根据其 lambda 捕获创建构造函数的概念是没有根据的,尽管这是 C++ Primer 中描述的方式(我在标准中找不到它的引用) ,因为即使我希望它在概念上有效,以下代码也不起作用:
int sz = 2;
auto a = [sz](){ return sz;};
decltype(a) b(10); //compiler error
decltype(a) b = a; //all good though
closure到lambda的关系类似于object到class.
C++11 标准 说 closure! 类型没有默认构造函数,这是正确的,因为它没有不要说它没有构造函数。
lambda 用于创建闭包。但是您引用的段落将更改 C++14.
ClosureType() = delete; // (until C++14)
ClosureType(const ClosureType& ) = default; // (since C++14)
ClosureType(ClosureType&& ) = default; // (since C++14)
Closure types are not DefaultConstructible. Closure types have
a deleted (until C++14)
no (since C++14)
default constructor. The copy constructor and the move constructor areimplicitly-declared (until C++14)
declaredas defaulted (since C++14)
and may be implicitly-defined according to the usual rules for copy constructors and move constructors.
因为赋值右边是一个临时对象(右值),所以'g'被移动赋值