1

Update3: please go to overloading operators for lamdas

I want to overload operator>> for lambdas in c++11/14 .

Here is a simple code:

#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);
}

But what I really want is this:

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

And get rid of function<F> .

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 >> 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;
}

But I still can't find out how to overload the >> operator for my simple example.

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

v >> [](int i) { } // calls the overload with one lambda argument
v >> [](int i1,int i2) { } // calls another overload with two lambda 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.

Community
  • 1
  • 1
amin
  • 3,672
  • 5
  • 33
  • 61

1 Answers1

2

Maybe the following piece of code will do the job:

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

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

You could use 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;
}

HTH

101010
  • 41,839
  • 11
  • 94
  • 168
  • In this simple example your solution works (thanks) . but if i want to overload the >> operator for different lambdas with different number of arguments then it wont . – amin May 11 '14 at 10:47
  • @amin how do you imagine passing the extra arbitrary number of input arguments for the Lambda expression in template binary `operator>>`? – 101010 May 11 '14 at 11:08
  • `vector` is just a simple example . i did not want to bring my actual code which is irrelevant to the question .( its a wrapper around sqlite C library , and i have a query class and i want to overload the >> operator to extract different number of columns form the query ) – amin May 11 '14 at 11:12
  • I think i did not explained what i really wanted , i have asked a new question , please take a look at http://stackoverflow.com/questions/23594969/operator-overloading-for-lambdas – amin May 11 '14 at 16:25