gpt4 book ai didi

c++ - 为 lambda 重载运算符>>

转载 作者:行者123 更新时间:2023-11-30 04:04:50 26 4
gpt4 key购买 nike

更新 3:请转至overloading operators for lamdas

我想重载operator>>对于 c++11/14 中的 lambda。

这是一个简单的代码:

#include<iostream>
#include<functional>
#include<vector>

using namespace std;

template <typename R,typename T>
void operator >> (vector<T>& v, function<R(T)> f){
for(auto& e : v)
f(e);
}

int main(){
vector<int> v = { 1,2,3,4,5,6,7};
auto l = [](int i) { cout << i*i << endl; };
v >> function<void(int)>(l);
}

但我真正想要的是:

v >> l; //(I)
v >> [](int i) { cout << i*i << endl; }; //(II)

并去掉 function<F> .

我从类似的问题中得到了一些想法 How to overload an operator for composition of functionals in C++0x?

特别是第二个答案中的代码重载了 >>两个 lambda 的运算符。

#include <iostream>

template <class LAMBDA, class ARG>
auto apply(LAMBDA&& l, ARG&& arg) -> decltype(l(arg))
{
return l(arg);
}

template <class LAMBDA1, class LAMBDA2>
class compose_class
{
public:
LAMBDA1 l1;
LAMBDA2 l2;

template <class ARG>
auto operator()(ARG&& arg) ->
decltype(apply(l2, apply(l1, std::forward<ARG>(arg))))
{ return apply(l2, apply(l1, std::forward<ARG>(arg))); }

compose_class(LAMBDA1&& l1, LAMBDA2&& l2)
: l1(std::forward<LAMBDA1>(l1)),l2(std::forward<LAMBDA2>(l2)) {}
};

template <class LAMBDA1, class LAMBDA2>
auto operator>>(LAMBDA1&& l1, LAMBDA2&& l2) ->compose_class<LAMBDA1, LAMBDA2>
{
return compose_class<LAMBDA1, LAMBDA2>
(std::forward<LAMBDA1>(l1), std::forward<LAMBDA2>(l2));
}

int main()
{
auto l1 = [](int i) { return i + 2; };
auto l2 = [](int i) { return i * i; };

std::cout << (l1 >> l2)(3) << std::endl;
}

但我还是不知道如何重载 >>我的简单示例的运算符。

更新:我实际上希望能够为不同的情况重载“>>”运算符具有不同数量参数的 lambda。

v >> [](int i) { } // calls the overload with one lambda argument
v >> [](int i1,int i2) { } // calls another overload with two lambda arguments

更新 2:我想我没有解释我真正想要的,我问了一个新问题operator overloading for lambdas?

我已经在那个问题中详细解释了这个问题。

最佳答案

也许下面的代码可以完成这项工作:

#include<iostream>
#include<functional>
#include<vector>

using namespace std;

template <typename R, typename T>
void operator >> (vector<T>& v, R f){
for(auto& e : v)
f(e);
}

int main(){
vector<int> v = { 1,2,3,4,5,6,7};
auto l = [](int i) { cout << i*i << endl; };
v >> function<void(int)>(l);
v >> l; //(I)
v >> [](int i) { cout << i*i << endl; }; //(II)
}

更新:

[OP update]: I actually want to be able to overload the '>>' operator for different lambdas with different number of arguments .

你可以使用 std::bind为了传递具有任意数量输入参数的不同 lambda(见下面的代码):

#include<iostream>
#include<functional>
#include<vector>

using namespace std;

template <typename T, typename F>
void operator >> (vector<T>& v, F f){
for (auto& e : v) f(e);
}

int main(){
using namespace std::placeholders;
vector<int> v = { 1, 2, 3, 4, 5, 6, 7 };
v >> std::bind([](int i, int j){ cout << i*j << " "; }, _1, 2);
cout << endl;
v >> std::bind(
[](int i, double d1, double d2)
{
cout << (static_cast<double>(i) * d1 / d2) << " ";
},
_1, 2.0, 3.0);
cout << endl;
return 0;
}

HTH

关于c++ - 为 lambda 重载运算符>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23591075/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com