Skip to content

Instantly share code, notes, and snippets.

@splinterofchaos
splinterofchaos / glambda-play.cpp
Last active December 10, 2015 04:28
Generic Lambda fun.
#include <vector>
#include <algorithm>
#include <iostream>
constexpr struct {
// Ignore perfect forwarding. Assume basic types.
template< class X, class Y >
auto operator () ( X x, Y y )
-> decltype(x*y)
@splinterofchaos
splinterofchaos / generic-lambda-tests.cpp
Last active December 9, 2015 23:49
Tests for Clang's implementation of polymorphic lambdas.
/* Last tested on:
* Ubuntu 12.
*
* clang version 3.2
* (https://github.com/faisalv/clang-glambda.git ff891bdfef1794e0e7ad4343a3f696da4785462a)
* (llvm/trunk 167560)
*/
#include <cstdio>
#include <vector>
@splinterofchaos
splinterofchaos / build log minus errors.txt
Created December 19, 2012 22:12
gclang compile attempt
make[1]: Entering directory `/home/soc/src/gllvm/build/lib/Support'
make[1]: Nothing to be done for `all'.
make[1]: Leaving directory `/home/soc/src/gllvm/build/lib/Support'
make[1]: Entering directory `/home/soc/src/gllvm/build/lib/TableGen'
make[1]: Nothing to be done for `all'.
make[1]: Leaving directory `/home/soc/src/gllvm/build/lib/TableGen'
make[1]: Entering directory `/home/soc/src/gllvm/build/utils'
make[2]: Entering directory `/home/soc/src/gllvm/build/utils/FileCheck'
make[2]: Nothing to be done for `all'.
make[2]: Leaving directory `/home/soc/src/gllvm/build/utils/FileCheck'
@splinterofchaos
splinterofchaos / list.cpp
Created December 15, 2012 01:03
Several of Haskell's Data.List functions implemented in C++ Data.List: http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html
#include <algorithm>
#include <vector>
#include <iterator>
#include <iostream>
template< class F, class X, class S >
constexpr X foldl( F&& f, X x, const S& s ) {
return std::accumulate (
std::begin(s), std::end(s),
@splinterofchaos
splinterofchaos / zip-map-tuple.cpp
Created December 12, 2012 14:17
More fun with tuples.
#include <tuple>
#include <iostream>
template< size_t ...i > struct IndexList {};
template< size_t ... > struct EnumBuilder;
// Increment cur until cur == end.
template< size_t end, size_t cur, size_t ...i >
@splinterofchaos
splinterofchaos / tuple-fun.cpp
Created December 11, 2012 05:21
fun with tuples.
#include <tuple>
#include <iostream>
template< size_t ...i > struct IndexList {};
template< size_t ... > struct EnumBuilder;
// Increment cur until cur == end.
template< size_t end, size_t cur, size_t ...i >
@splinterofchaos
splinterofchaos / does-it-run.cpp
Created December 9, 2012 23:09
Does it run?
#include <tuple>
template< class X > struct Hold {
X x;
constexpr Hold( X x ) : x(std::move(x)) { }
};
constexpr auto a = Hold<int>( 1 );
constexpr auto b = Hold<std::tuple<int>>( std::tuple<int>(1) );
@splinterofchaos
splinterofchaos / moving-lambdas.cpp
Created December 1, 2012 14:24
To move a lambda.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void print_vec( const char* const name, const vector<int>& v ) {
cout << name << " = ";
@splinterofchaos
splinterofchaos / arrow.cpp
Created November 30, 2012 14:33
Arrows and Kleislies
#include <memory>
#include <iostream>
#include <sstream>
#include <utility>
#include <algorithm>
#include <iterator>
/* MakeT T : X -> T X */
template< template<class...> class T > struct MakeT {
@splinterofchaos
splinterofchaos / template error
Created November 28, 2012 18:31
An error message for one line of code involving template trickery.
examples.cpp: In function 'int main()':
examples.cpp:234:10: warning: variable 'o' set but not used [-Wunused-but-set-variable]
In file included from /usr/include/c++/4.7/bits/stl_algo.h:68:0,
from /usr/include/c++/4.7/algorithm:63,
from Pure.h:4,
from examples.cpp:2:
/usr/include/c++/4.7/functional: In instantiation of 'static _Res std::_Function_handler<_Res(_ArgTypes ...), _Functor>::_M_invoke(const std::_Any_data&, _ArgTypes ...) [with _Res = std::unique_ptr<std::pair<int, int> >; _Functor = pure::arrow::KleisliF<std::unique_ptr, int, std::pair<int, int>, pure::Composition<pure::arrow::KleisliF<std::unique_ptr, std::pair<int, int>, std::pair<int, int>, pure::BComposition<pure::Part<pure::monad::LiftM, pure::MakeT<std::pair> >, std::function<std::unique_ptr<int>(int)>, std::function<std::unique_ptr<int>(int)> > >, pure::arrow::Duplicate> >; _ArgTypes = {int}]':
/usr/include/c++/4.7/functional:2298:6: required from 'std::function<_Res(_ArgTypes ...)>::fun