了解Lambda闭包类型如何删除默认构造函数 [英] Understanding how Lambda closure type has deleted default constructor
问题描述
从5.1.2
[19]与lambda表达式关联的闭包类型已删除3)默认构造函数和一个删除的
复制赋值运算符。它有一个隐式声明的复制构造函数(12.8),可能有一个隐式声明的
移动构造函数(12.8)。 [注意:复制/移动构造函数以相同的方式隐式定义
,因为任何其他隐式声明的复制/移动构造函数将被隐式定义。 -end note]
[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表达式转换为未命名未命名类的对象。如果默认构造函数被删除,我如何定义不包含lambda捕获的lambda函数的对象?
I'm reading through C++ Primer 14.8.1 which explains lambda expressions being translated by the compiler in to an unnamed object of an unnamed class. How is it I can I define objects of lambda functions which do not contain a lambda capture, if the default constructor is deleted?
auto g = [](){};
这在概念上与...不一样...
Is this not conceptually the same as...
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 被定义,并且可以初始化这样的对象(只要参数被传递)。但是如果没有捕获并且默认构造函数被删除,概念上看起来不一致,可以创建一个lambda类对象。
If there was a capture then a constructor other than the default constructor would be defined and it would be okay to initialise objects of such (as long as a parameter was passed). But if there is no capture and the default constructor is deleted, it doesn't seem consistent conceptually that a lambda class object can be created.
编辑:嗯,也许lambda类创建构造函数根据它的lambda捕获是毫无根据的,尽管这是如何描述在C ++ Primer(我可以找到没有在标准中的报价),因为下面的代码不工作,即使我期望它概念上:
Hmm, maybe the conception that the lambda class creates constructors depending on its lambda captures is unfounded despite this being how it's described in C++ Primer (I can find no quote of it in the standard), because the following code does not work even though I would expect it to conceptually:
int sz = 2;
auto a = [sz](){ return sz;};
decltype(a) b(10); //compiler error
decltype(a) b = a; //all good though
推荐答案
>封闭到 lambda 类似于对象到类。
C ++ 11标准表示 closure!类型没有默认构造函数,这是正确的,因为它没有说它没有构造函数。
The C++11 standard says that the closure! type has no default constructor, and that is correct because it doesn't say it has no constructor.
lambda用于创建闭包。但是你引用的parapgraph会改变为 C ++ 14 。
The lambda is used to create a closure. But your quoted parapgraph will change for C++14.
ClosureType() = delete; // (until C++14)
ClosureType(const ClosureType& ) = default; // (since C++14)
ClosureType(ClosureType&& ) = default; // (since C++14)
a href =http://en.cppreference.com/w/cpp/concept/DefaultConstructible =nofollow> DefaultConstructible 。关闭类型有
被删除(直到C ++ 14)
否(因为C ++ 14)
默认构造函数。复制构造函数和移动构造函数隐式声明(直到C ++ 14)
声明为默认值(因为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.
http://en.cppreference.com/w/cpp/language/lambda
这篇关于了解Lambda闭包类型如何删除默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!