public
Last active

To move a lambda.

  • Download Gist
moving-lambdas.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
 
 
#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
void print_vec( const char* const name, const vector<int>& v ) {
cout << name << " = ";
 
for( int x : v ) {
cout << x << " ";
}
 
cout << endl;
}
 
template< class F, class X >
struct Part {
F f;
X x;
 
template< class _F, class _X >
constexpr Part( _F&& f, _X&& x )
: f(forward<_F>(f)), x(forward<_X>(x))
{
}
 
template< class ... Xs >
auto operator () ( Xs&& ...xs )
-> decltype( f(x,declval<Xs>()...) )
{
return f( x, forward<Xs>(xs)... );
}
};
 
template< class F, class X >
constexpr Part<F,X> closet( F f, X x ) {
return Part<F,X>( std::move(f), std::move(x) );
}
 
std::vector<int> add_all( std::vector<int>& v, int x ) {
// Arbitrarily move v.
auto w = std::move(v);
 
std::transform( w.begin(), w.end(), w.begin(),
closet(std::plus<int>(),x) );
 
return w;
}
 
std::vector<int> add_all2( std::vector<int> w, int x ) {
std::transform( w.begin(), w.end(), w.begin(),
closet(std::plus<int>(),x) );
 
return w;
}
 
// originate returns a partial application of add_all with a vector.
std::function<std::vector<int>()> bad_originate() {
std::vector<int> v = { 1, 2, 3, 4, 5 };
 
// Move v, avoid upward funarg problem.
// en.wikipedia.org/wiki/Funarg_problem#Upwards_funarg_problem
return [&]{ return v; };
}
 
// originate returns a partial application of add_all with a vector.
using Origin = Part<decltype(add_all)*,std::vector<int>>;
Origin originate() {
std::vector<int> v = { 1, 2, 3, 4, 5 };
 
// Move v, avoid upward funarg problem.
// en.wikipedia.org/wiki/Funarg_problem#Upwards_funarg_problem
return closet( add_all, std::move(v) );
}
 
template< class F, class G > struct Composition {
F f = F();
G g = G();
 
Composition() { }
Composition( F f, G g)
: f(move(f)), g(move(g)) { }
 
template< class X, class ...Y >
auto operator () ( X&& x, Y&& ...y )
-> decltype( f(g(declval<X>()), declval<Y>()...) )
{
return f( g( forward<X>(x) ), forward<Y>(y)... );
}
};
 
template< class F, class G >
Composition<F,G> comp( F f, G g ) {
return Composition<F,G>( std::move(f), std::move(g) );
}
 
constexpr struct Mover {
template< class X >
X&& operator () ( X& x ) const {
return std::move(x);
}
} mover{};
 
using MoveAddAll = decltype( comp(add_all2,mover) );
using Origin2 = Part< MoveAddAll, std::vector<int> >;
Origin2 originate2() {
std::vector<int> v = { 1, 2, 3, 4, 5 };
 
// Move v, avoid upward funarg problem.
// en.wikipedia.org/wiki/Funarg_problem#Upwards_funarg_problem
return Origin2( comp(add_all2,mover), std::move(v) );
}
 
int main()
{
auto bad = bad_originate();
print_vec( "bad : ", bad() );
 
cout << "version 1:\n";
Origin f = originate();
print_vec( "original : ", f.x );
print_vec( "added 10 : ", f(10) );
print_vec( "original : ", f.x );
 
cout << "\nversion 2:\n";
Origin2 g = originate2();
print_vec( "original : ", g.x );
print_vec( "added 10 : ", g(10) );
print_vec( "original : ", g.x );
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.