重载运算符>>对于lambdas

时间:2014-05-11 09:41:43

标签: c++ c++11 lambda operator-overloading

UPDATE3: 请转到overloading operators for lamdas

我想在c ++ 11/14中为lambdas重载operator>>

这是一个简单的代码:

#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?

特别是第二个答案的代码会使>>运算符重载两个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;
}

但我仍然无法找到如何为>>运算符重载我的简单示例。

更新 我实际上希望能够超载&#39;&gt;&gt;&#39;不同的运营商 lambdas具有不同数量的参数。

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

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

我已在该问题中详细解释了问题。

1 个答案:

答案 0 :(得分:2)

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

#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更新]:我实际上希望能够重载“&gt;&gt;”不同的运营商   lambdas具有不同数量的参数。

您可以使用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