C++ 如何从嵌套的lambda表达式函子类创建
我已经开始学习lambda表达式和嵌套lambda表达式:C++ 如何从嵌套的lambda表达式函子类创建,c++,lambda,functor,C++,Lambda,Functor,我已经开始学习lambda表达式和嵌套lambda表达式: auto Suma=[](int x)->function<int(int)> { return [x](int y) { return x+y; }; }; auto Mult=[](function<int(int)>&f, int z) { return f(z)*2
auto Suma=[](int x)->function<int(int)>
{
return [x](int y)
{
return x+y;
};
};
auto Mult=[](function<int(int)>&f, int z)
{
return f(z)*2;
};
.cpp:
class Suma
{
public:
int operator()(int x);
};
class Inner
{
Suma function;
public:
Inner(int x);
int operator()(int k);
};
class Mult
{
public:
int operator()(Suma function,int z);
};
int Suma::operator()(int x)
{
return x;
}
Inner::Inner(int x)
{
function.operator()(x);
}
int Inner::operator()(int k)
{
return function.operator()+k;
}
int Mult::operator()(Suma function,int z)
{
return (function.operator())*(2);
}
我面临的主要问题是在尝试将函数从一个相关类包含到另一个相关类时。我不完全理解它是如何通过课堂联系起来的。请告诉我它应该如何工作。您的类没有反映lambda的结构 内部lambda返回一个函数,并将其参数添加到捕获中。 因此,您的
内部
类不应依赖于Suma
,定义如下:
// definition in .h
class Inner
{
int myx;
public:
Inner(int x); // this represents the capture of the lambda
int operator()(int k); // this is the function of adding k to the catpture
};
// implementation in .cpp
Inner::Inner(int x)
{
myx = x;
}
int Inner::operator()(int k)
{
return myx + k;
}
然后,Suma应返回带有x集的内部:
// definition in .h
class Suma
{
public:
Inner operator()(int x);
};
// implementation in .cpp
Inner Suma::operator()(int x)
{
return Inner(x);
};
使用lambda,当您计算Suma(3)
时,您将获得一个接受整数并向其添加3的函数。如果你计算Suma(3)(2)
,你会得到5
对于模拟lambda的类,您将执行相同的操作:
Suma f; // that is an object, like "auto suma"
cout << f(3)(2) <<endl; // f(3) is in fact an object Inner(3) wich is then exectued with argument 2.
顺便说一句,如上所述,您不需要显式地调用运算符():只需提供括号之间的参数 您的类不反映lambda的结构 内部lambda返回一个函数,并将其参数添加到捕获中。 因此,您的
内部
类不应依赖于Suma
,定义如下:
// definition in .h
class Inner
{
int myx;
public:
Inner(int x); // this represents the capture of the lambda
int operator()(int k); // this is the function of adding k to the catpture
};
// implementation in .cpp
Inner::Inner(int x)
{
myx = x;
}
int Inner::operator()(int k)
{
return myx + k;
}
然后,Suma应返回带有x集的内部:
// definition in .h
class Suma
{
public:
Inner operator()(int x);
};
// implementation in .cpp
Inner Suma::operator()(int x)
{
return Inner(x);
};
使用lambda,当您计算Suma(3)
时,您将获得一个接受整数并向其添加3的函数。如果你计算Suma(3)(2)
,你会得到5
对于模拟lambda的类,您将执行相同的操作:
Suma f; // that is an object, like "auto suma"
cout << f(3)(2) <<endl; // f(3) is in fact an object Inner(3) wich is then exectued with argument 2.
顺便说一句,如上所述,您不需要显式地调用运算符():只需提供括号之间的参数 你到底不明白什么?我不明白它们是如何通过类连接起来的。因为lambdas的实现是通过它完成的。。。这就是为什么我会问在这种情况下它应该是什么样子的…你具体不明白什么?我不明白它们是如何通过类连接起来的。因为lambdas的实现是通过它完成的。。。这就是为什么我问它在这种情况下应该是什么样子…克里斯托弗,非常感谢,但它不起作用(((错误254:错误C44:缺少类型指定符-int假设。注意:C++不支持缺省值。int@Melany我编辑了一些额外的解释和一个联机代码的链接。@Melany我在MSVC中测试了联机代码:它也可以正常工作,没有错误。但是,您的Mult有缺陷并导致错误。因此我编辑了m我会回答你的问题。非常感谢,你真的让我明白了。这真的很有帮助,再次感谢你!!!克里斯托夫,非常感谢,但它不起作用(((错误254:错误C44:缺少类型指定符-int假设。注意:C++不支持缺省值。int@Melany我编辑了一些额外的解释和一个联机代码的链接。@Melany我在MSVC中测试了联机代码:它也可以正常工作,没有错误。但是,您的Mult有缺陷并导致错误。因此我编辑了m我的答案是解释它的问题。非常感谢,你真的让我明白了。这真的很有帮助,再次感谢你!!!