当包含lambda的类型被赋值时会发生什么? [英] What happens when a type containing a lambda gets assigned?
问题描述
说我有某种类型的包装函数,可能是lambda函数:
Say I have some sort of type wrapping up a function, maybe a lambda function:
template<typename Function>
struct my_struct
{
Function f;
my_struct(const Function &f) : f(f) {}
};
当分配此类型的实例时会发生什么?我的理解是,lambdas是不可变的,并且已经删除了赋值运算符。
What happens when an instance of this type is assigned? My understanding is that lambdas are immutable, and have deleted assignment operators.
然而,当我在下面的代码片段中指定一个对象时,不会发出错误:
Yet, when I assign to an object this type in the code snippet below, no error is emitted:
// a structure which contains a function;
// possibly a lambda function
template<typename Function>
struct my_struct
{
Function f;
my_struct(const Function &f) : f(f) {}
// XXX adding this assignment operator causes an error
//my_struct &operator=(const my_struct &other)
//{
// f = other.f;
// return *this;
//}
};
template<typename Function>
my_struct<Function> make_struct(const Function &f)
{
return my_struct<Function>(f);
}
int main()
{
// create some lambda
auto lambda = [](int x){return x;};
// make a struct containing a copy of the lambda
auto x = make_struct(lambda);
// try to assign to the struct, which
// presumably assigns to the enclosed lambda
x = make_struct(lambda);
return 0;
}
添加注释的赋值运算符会产生错误, p>
Adding the commented-out assignment operator yields an error, as expected:
$ g++-4.6 -std=c++0x test.cpp
test.cpp: In member function ‘my_struct<Function>& my_struct<Function>::operator=(const my_struct<Function>&) [with Function = main()::<lambda(int)>, my_struct<Function> = my_struct<main()::<lambda(int)> >]’:
test.cpp:34:25: instantiated from here
test.cpp:13:5: error: use of deleted function ‘main()::<lambda(int)>& main()::<lambda(int)>::operator=(const main()::<lambda(int)>&)’
test.cpp:27:18: error: a lambda closure type has a deleted copy assignment operator
成员变量?这似乎是一个合理的事情想试试。例如,考虑组合一个lambda和 boost :: transform_iterator
。
So, is it possible to create assignable types with lambda member variables? This seems like a reasonable thing to want to try. Consider combining a lambda with boost::transform_iterator
, for example.
推荐答案
你就近了。 lambda具有隐式复制构造函数,并且可能具有 - 取决于捕获的值 - 隐式移动构造函数。它有一个删除的副本赋值运算符。
You're close. A lambda has an implicit copy-constructor and may have — depending on the captured values — an implicit move-constructor. It has a deleted copy-assignment operator.
换句话说,你可以构造它,但你不能分配它。如果你正在寻找一个泛型函数对象,你想使用 std :: function<>
。
In other words, you may construct it, but you may not assign it. If you're looking for a generic function object, you want to use std::function<>
. It emulates functions as first-class values.
请注意,immutable不同于assignable。当一个lambda被称为mutable时,这意味着它的函数调用体可以修改lambda的成员(即函数不是 const
):
Note that immutable is different from assignable. When a lambda is called mutable, that means its function-call body can modify the members of the lambda (i.e., the function is not const
):
int i = 0;
auto okay = [=](int x) mutable { i += x; };
auto error = [=](int x) { i += x; };
这些都是可复制构造且不可分配。
Each of these is copy-constructable and non-assignable.
这篇关于当包含lambda的类型被赋值时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!