Lambda functions as a class

Is it possible to accept two different types of lambda functions as members of a class without knowing the arguments of their template in advance?

struct two_functors { std::function<???> a; std::function<???> b; ... }; 

To make something like this possible:

 void main(){ vector<two_functors> many_functors; int a = 2; int b = 3; double c = 4.7; double d = 8.4; two_functors add_and_subtract; add_and_subtract.a = [a, b](int x, int y){cout << x + y << endl;}; add_and_subtract.b = [c, d](double x, double y){cout << x - y << endl;}; two_functors multiply_and_divide; multiply_and_divide.a = [c, d](double x, double y){cout << x * y << endl;}; multiply_and_divide.b = [a, b](int x, int y){cout << x / y << endl;}; many_functors.push_back(add_and_subtract); many_functors.push_back(multiply_and_divide); for (auto functors : many_functors){ functors.a(); functors.b(); } } 
+7
source share
4 answers

If you just want to build two_functors at different times, but execute them sequentially sequentially, you can just use the captured data.

 struct two_functors { function<void ()> a; function<void ()> b; }; int main() { vector<two_functors> many_functors; int a = 2; int b = 3; double c = 4.7; double d = 8.4; two_functors add_and_subtract { [a, b](){cout << a + b << endl;}, [c, d](){cout << c - d << endl;} }; two_functors multiply_and_divide { [c, d](){cout << c * d << endl;}, [a, b](){cout << a / b << endl;} }; many_functors.push_back(add_and_subtract); many_functors.push_back(multiply_and_divide); for (auto functors : many_functors){ functors.a(); functors.b(); } } 
+3
source

This is essentially tuple . You can see how the interface is implemented for this.

 template< class F0, class F1 > struct two_functors { F0 func0; F1 func1; }; template< class F0, class F1 > two_functors<F0, F1> make_two_functor( F0&& f0, F1&& f1 ) { // Added [std::forward][2] return two_functors<F0,F1>( std::forward<F0>(f0), std::forward<F1>(f1) ); } 
+2
source

An alternative to Stephen would be to use an intermediate "umbrella" class.

EDIT: just compiled the example in g ++ (GCC) 4.5.3

  #include <functional> #include <iostream> using namespace std; class myfunction { }; template <typename T> class specificFunction : public myfunction { public: function<T> f; specificFunction(function<T> pf) { f = pf; } }; struct two_functors { myfunction* a; myfunction* b; }; int main() { myfunction* f = new specificFunction<void(int,int)> ([](int a, int b) { cout << a << " - " << b << endl; }); myfunction* f2 = new specificFunction<void(double,int)> ([](double a, int b) { cout << a << " - " << b << endl; }); two_functors tf; tf.a = f; tf.b = f2; ((specificFunction<void(int,int)>*)(tf.a))->f(4,5); ((specificFunction<void(double,int)>*)(tf.b))->f(4.02,5); } 
0
source

Not an attempt to answer (I just need a formatting style), just a variation of Stephen's suggestion

 template<typename A, typename B> two_functors<A,B> make_two_functors(A&& a, B&& b) { return two_functors<A,B> {a, b}; } 

Are there any disadvantages compared to using std::forward<T> ?

Btw - I somehow wish such "manufacturers" to disappear from C ++ 11.

0
source

All Articles