public
Last active

Tests for Clang's implementation of polymorphic lambdas.

  • Download Gist
generic-lambda-tests.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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
/* Last tested on:
* Ubuntu 12.
*
* clang version 3.2
* (https://github.com/faisalv/clang-glambda.git ff891bdfef1794e0e7ad4343a3f696da4785462a)
* (llvm/trunk 167560)
*/
 
#include <cstdio>
#include <vector>
#include <tuple>
#include <algorithm>
#include <iterator>
 
#include <iostream>
#include <string>
#include <memory>
#include <locale>
 
auto add = []( auto a, auto b ) a + b;
 
auto inc = []( auto x ) ++x;
 
// Composition.
auto comp =
[]( auto f, auto g )
[=]( auto x ) f( g(x) );
 
// Partial application.
auto part =
[]( auto f, auto x )
[=]<class ...Y>( Y&& ...y )
f( x, std::forward<Y>(y)... );
 
auto curry3 =
[]( auto f )
[=](auto x) [=](auto y) [=](auto z)
f(x,y,z);
 
auto for_each = []( auto f, auto& s ) {
for( auto& x : s )
f(x);
};
 
template< class X >
using Singleton = std::tuple<X>;
 
auto singleton = []<class X>( X x ) Singleton<X>( std::move(x) );
 
auto singleton2 =
[]( auto x ) Singleton <
decltype(x)
> ( std::move(x) );
 
// Get the singleton value.
auto svalue = []( auto single ) std::get<0>(single);
 
// Map the singleton.
auto smap = []( auto f, auto single )
singleton( f( svalue(single) ) );
auto smap2 = []( auto f, auto single )
singleton2( f( svalue(single) ) );
 
template< class X, class Y >
Singleton<Y> operator >> ( const Singleton<X>&, Singleton<Y> b ) {
return std::move(b);
}
 
template< class X, class F >
auto operator >>= ( const Singleton<X>& x, const F& f )
-> typename std::result_of< F(X) >::type
{
return f( svalue(x) );
}
 
template< class F, class G, class ...X >
constexpr auto sfinae( const F& f, const G&, X&& ...x )
-> decltype( f( std::forward<X>(x)... ) )
{
return f( std::forward<X>(x)... );
}
 
template< class F, class G, class ...X >
constexpr auto sfinae( const F&, const G& g, X&& ...x )
-> decltype( g( std::forward<X>(x)... ) )
{
return g( std::forward<X>(x)... );
}
 
template< class F > struct Forwarder : F {
constexpr Forwarder( const F& f ) : F(f) { }
 
constexpr operator F() { return *this; }
//using F::operator();
};
 
template< class R, class ...X > struct Forwarder<R(*)(X...)> {
using type = R(*)(X...);
type f;
 
constexpr Forwarder( type f ) : f(f) { }
 
constexpr R operator () ( X... x ) {
return f( std::forward<X>(x)...);
}
};
 
template< class F, class G >
struct Overloaded : Forwarder<F>, Forwarder<G> {
constexpr Overloaded( const F& f, const G& g )
: Forwarder<F>(f), Forwarder<G>(g)
{
}
 
constexpr operator Forwarder<F>() { return *this; }
constexpr operator Forwarder<G>() { return *this; }
};
 
template< class F > F overload( F&& f ) {
return std::forward<F>(f);
}
 
template< class F, class G, class ...H,
class O1 = Overloaded<F,G> >
auto overload( const F& f, const G& g, const H& ...h )
-> decltype( overload(O1(f,g),h...) )
{
return overload( O1(f,g), h... );
}
 
 
 
template< class X, class F >
struct OverloadType : F {
OverloadType( F f ) : F(f) { }
 
using result_type = typename std::result_of< F(X) >::type;
 
result_type operator () ( X x ) const {
return F::operator()(x);
}
 
operator F() const { return *this; }
};
 
template< class ...X, class F >
auto overload_set( const F& f )
-> decltype( overload(OverloadType<X,F>(f)...) )
{
return overload( OverloadType<X,F>(f)... );
}
 
auto cout1 = []( const auto& x ) { std::cout << x; };
 
struct sequence_tag {};
struct other_tag {};
 
template< class S > auto get_tag(const S& s) -> decltype( std::begin(s), sequence_tag{} );
template< class S > auto get_tag(...) -> other_tag;
 
template< class Tag, class F >
struct OverloadTag : F {
constexpr OverloadTag( const F& f ) : F(f) { }
 
template< class X >
constexpr auto operator () ( X&& x ) const
-> typename std::enable_if <
std::is_same< Tag, decltype(get_tag(x)) >::value,
typename std::result_of< F(X) >::type
>::type
{
return F::operator()( std::forward<X>(x) );
}
 
operator F() const { return *this; }
};
 
template< class Tag, class F, class O = OverloadTag<Tag,F> >
O overload_tag( const F& f ) {
return O( f );
}
 
void print_int( int x ) { std::cout << "i_" << x; }
 
auto cprint = overload (
[]{}, // No input? No output.
 
// Cannot be disambiguated from Singleton and vector overload.
//[]( const auto& x ) { std::cout << x },
 
&print_int,
 
overload_set</*int,*/char,float,double,
const char* const,
const std::string&>( cout1 ),
 
[]<class X>( const Singleton<X>& s ) {
std::cout << "{" << svalue(s) << "}";
},
 
overload_tag< sequence_tag > (
[]( const auto& s ) {
std::cout << "[ ";
for( const auto& x : s )
std::cout << x << ' ';
std::cout << ']';
}
)
);
 
// Variadic void unary.
auto vvu_impl = overload (
[] (auto,auto) {},
[]<class X, class ...Y>( const auto& self, const auto& u,
X&& x, Y&& ...y )
{
u( std::forward<X>(x) );
self( self, u, std::forward<Y>(y)... );
}
);
 
// vvu(vvu,f,x,y...) = f(x); f(y)...
auto vvu = []( const auto& u )
[&]<class ...X>( const X& ...x )
vvu_impl( vvu_impl, u, x... );
 
// Variadic print.
// vprint(x,y...) = cprint(x); cprint(y)...
auto vprint = vvu( cprint );
 
auto print_line = []<class ...X>( const X& ...x )
vprint( x..., '\n' );
 
auto Make = []<class X>( auto y ) X(y);
 
auto Const = []<int n>(auto) n;
 
auto If = []<bool B>( auto x, auto y ) B ? x : y;
auto boolTest =
[]< class X, bool isInt = std::is_same<X,int>::value >
( const X& x ) If.operator()<isInt>( x*2, x/2 );
 
auto x = boolTest(10);
 
auto boolTest2 =
[]< class X, bool isInt = std::is_same<X,int>::value >
( X x ) If.operator()<isInt>( x*2, x/2 );
 
auto boolTest3 =
[]( auto x ) {
constexpr bool b = std::is_same< decltype(x), int >::value;
return If.operator()<b>( x*2, x/2 );
};
 
 
auto chainl_impl = overload (
[]( auto self, auto b, auto r ) { return r; },
[]<class ...Z>( auto self, auto b, auto x, auto y, Z ...z )
self( self, b, b(x,y), z... )
);
 
auto chainl = []( auto b )
[=]<class ...X>( const X& ...x )
chainl_impl( chainl_impl, b, x... );
 
auto compose = chainl( comp );
auto sum = chainl( add );
 
auto min = overload (
[]( auto&& a, auto&& b ) { return a > b ? std::move(b) : std::move(a); },
 
//[]( auto& a, auto& b ) -> decltype(a) a > b ? b : a, // expression result unused?
[]( auto& a, auto& b ) -> decltype(a) { return a > b ? b : a; },
 
[]( const auto& a, const auto& b ) -> decltype(a) a > b ? b : a
);
 
void f() {
const auto g = [](auto x, auto y) x + y;
 
// Inner lambda alone cannot capture g.
//auto f = []() [g]() g;
//auto _g = f()();
 
//std::cout << "ten : " << _g(5,5) << std::endl;
}
 
 
// Error here. (OK)
//auto embed = []( auto a ) []( auto b ) a + b;
//auto embed12 = embed(1)(2);
 
constexpr int max(int x, int y) { return x > y ? x : y; }
 
 
 
int two = 2;
auto plus_two = ([](auto x) [=](auto y) x + y)(two);
auto five = ([](auto x) [=](auto y) x + y)(two)(3);
 
auto zzz = ([](auto x) ([=](auto y) ([](auto x,auto y)x+y)(x,y))(x));
 
int main() {
f();
 
std::cout << "5+2 = " << plus_two(5) << std::endl;
std::cout << "5+2 = " << five << std::endl;
std::cout << "5+2 = " << zzz(5) << std::endl;
 
int low = 5, high = 10;
const int& low2 = min(low,high);
const int& low3 = min(low2,high);
 
std::cout << "\nmin(5,10) = " << low3 << std::endl;
low++;
std::cout << "min(6,10) = " << low3 << std::endl;
 
using ivoid = void (*) ( const int& );
using svoid = void (*) ( const std::string& );
 
auto cout_is = overload( ivoid(cout1), svoid(cout1) );
cout_is("\nten : "); cout_is(10); cout_is("\n\n");
 
print_line( "10(2) = ", Const.operator()<10>(2) );
print_line( "Sum of 9 and 10 : ", sum(9,10) );
 
print_line( "Sum of 1, 2, and 3 : ", curry3(sum)(1)(2)(3) );
 
print_line( "the char 'c': ", 'c' );
 
auto addThree = compose( inc, inc, inc );
print_line( "0 + 3 = ", addThree(0) );
 
auto parter = part(comp,inc);
print_line( "0 + 2 = ", parter(inc)(0) );
 
std::vector<int> v = { 1, 2, 3, 4, 5 };
print_line( "v = ", v );
 
// Cannot recurse cprint.
//std::vector<std::vector<int>> vv = { v, v, v, v };
//print_line( "vv = ", vv );
 
puts("");
 
Singleton<char> schar{ 'a' };
 
auto ord = []( char c ) (int) c;
 
print_line( "ord {'a'} = ", smap(ord,schar) );
print_line( "ord {'a'} = ", smap2(ord,schar) );
 
print_line( "\nx <- {'a'}"
"\ny <- {'b'}"
"\n{x + y} = ",
singleton('a') >>= [&](char a)
singleton('b') >>= [&](char b)
singleton( char(a + b) )
);
 
print_line( "True = ", If.operator()<true >(1,0) );
print_line( "False = ", If.operator()<false>(1,0) );
//print_line( "2 = ", If.operator()<3>(1,0) ); // would this work anyway?
 
puts( "\nLet f x = 2x if x is an int.\n"
"Let f x = x/2 otherwise." );
 
print_line( "f 10 = ", boolTest(10) );
print_line( "f 10 = ", boolTest2(10) );
print_line( "f 10 = ", boolTest3(10) );
print_line( "f 10.0 = ", (int)boolTest(10.0) );
 
print_line();
 
[]<class X>( X x ) -> typename std::enable_if <
std::is_fundamental<X>::value, X
>::type { return x; }(5);
}
numerics.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
 
#include <functional>
#include <iostream>
 
struct Natural {
int x;
 
Natural( int x ) : x(x) { }
operator int() { return x; }
 
std::function<Natural(int)> plus = ([](auto x) [=](auto y) x + y)(x);
};
 
auto part =
[]( auto f, auto x )
[=]<class ...Y>( Y&& ...y )
f( x, std::forward<Y>(y)... );
 
auto partial_add = []<class R>( auto x )
[=]( auto y ) R( x + y );
 
template< class N > struct Num {
N x;
 
Num( N x ) : x(x) { }
 
operator int() { return x; }
 
std::function<Num(int)> plus = ([](auto x) [=](auto y) x + y)(x);
 
//std::function<Num(int)> plus = part (
// //[](auto x, auto y) x + y, // No matching call to part?
// [](auto x, auto y) { return x + y; },
// x
//);
};
 
template< std::size_t k > struct Mod {
int x;
 
std::function<Mod(int)> plus = [=]( auto y ) x + y;
 
std::function<Mod(int)> minus;
 
std::function<Mod(int)> mult = [=]( auto n ) {
Mod res = 0;
while( n-- )
res = res.plus(x);
return res;
};
 
using func_type = Mod(*)(int,int);
static func_type div;
 
constexpr Mod( int x )
: x( x % k )
, minus( [=](auto y) x - y )
 
// Ok!
//minus( ([](auto x)[=](auto y)x-y)(x) )
{
}
 
constexpr Mod operator () ( int x ) {
return Mod( x );
}
 
constexpr operator int() { return x; }
};
 
template< std::size_t k >
using ModFn = Mod<k>(*)(int,int);
 
template< std::size_t k >
ModFn<k> Mod<k>::div =
[](int x, int y) Mod<k>(x / y);
 
constexpr int constexpr_min( int x, int y ) { return x > y ? y : x; }
 
auto mod0 = []<std::size_t k, std::size_t j>( Mod<k>, Mod<j> )
Mod< constexpr_min(k,j) >( 0 );
 
auto addk = []( auto a, auto b )
mod0(a,b)( a + b );
 
int main() {
std::cout << "3 + 5 (Natural) = " << (int)Natural(3).plus(5) << std::endl;
std::cout << "3 + 5 (Num int) = " << (int)Num<int>(3).plus(5) << std::endl;
 
std::cout << "3 (mod 4) + 6 (mod 10) = " << addk(Mod<4>(3),Mod<10>(6)) << std::endl;
std::cout << "3 (mod 4) + 6 = " << Mod<4>(3).plus(6) << std::endl;
std::cout << "3 (mod 4) - 6 = " << Mod<4>(3).minus(6) << std::endl;
std::cout << "2 (mod 5) * 7 = " << Mod<5>(2).mult(7) << std::endl;
// When run, emits "7078 illegal hardware instruction (core dumped)"
// WTF!?
//std::cout << "10 / 2 (mod 3) = " << Mod<3>::div(10,2) << std::endl;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.