重载运算符>>为兰布达 [英] Overloading operator>> for lambdas
问题描述
Update3:
请前往为lamdas重载运算子
我想在c ++ 11/14中重载 operator>>
for lambdas。 / p>
这里是一个简单的代码:
#include< iostream>
#include< functional>
#include< vector>
using namespace std;
template< typename R,typename T>
void operator>> (向量>& v,函数< R(T)> f){
for(auto& e:v)
f
}
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& code>。
我有一个类似问题的想法如何重载运算符以组合C ++ 0x中的函数?
特别是第二个回答的代码重载了两个lambdas的>>
运算符。
#include< iostream>
template< class LAMBDA,class ARG>
自动应用(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& ))); }
compose_class(LAMBDA1&& l1,LAMBDA2&& l2)
:l1(std :: forward< LAMBDA1>(l1)),l2(std :: forward< LAMBDA2> ;(12)){}
};
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;
}
但我还是找不到如何重载<$ c $
更新:我实际想要的是可以为具有不同数量参数的不同
lambdas重载'>>'运算符。
v ;> [](int i){} //调用带有一个lambda参数的重载
v>> [](int i1,int i2){} //调用具有两个lambda参数的另一个重载
strong> Update2: 我已经解释了问题中的细节问题。 [ OP更新]:我实际上想要为不同的$ b重载'>>' $ b lambdas with different number of arguments。 您可以使用 HTH Update3:
please go to overloading operators for lamdas I want to overload Here is a simple code: But what I really want is this: And get rid of I have got some ideas from a similar question How to overload an operator for composition of functionals in C++0x? Especially this code from the second answer overloads the But I still can't find out how to overload the Update:
I actually want to be able to overload the '>>' operator for different
lambdas with different number of arguments . Update2:
I think i did not explained what i really wanted , i have asked a new question operator overloading for lambdas? I have explained the problem with details in that question. Maybe the following piece of code will do the job:
[OP update]: I actually want to be able to overload the '>>' operator for different
lambdas with different number of arguments . You could use HTH 这篇关于重载运算符>>为兰布达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
我想我没有解释我真正想要的,我提出了一个新问题
#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)
}
更新: h2>
std :: bind
,以便传递具有任意数量输入参数的不同lambdas见下面的代码):
#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(){
使用命名空间std :: placeholder;
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;
}
operator>>
for lambdas in c++11/14 .#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>
.>>
operator for two lambdas.#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;
}
>>
operator for my simple example.v >> [](int i) { } // calls the overload with one lambda argument
v >> [](int i1,int i2) { } // calls another overload with two lambda arguments
#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)
}
Update:
std::bind
in order to pass different lambdas with arbitrary number of input arguments (see code below):#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;
}