Skip to content

Instantly share code, notes, and snippets.

@Redchards
Redchards / CallableTraits.hxx
Last active January 9, 2016 13:58
Very simple (albeit confusing) implementation of function currying in C++.
#ifndef IS_CALLABLE_HXX
#define IS_CALLABLE_HXX
#include <functional>
#include <tuple>
template<class ...>
using void_t = void;
// In fact, operator() is defined for the standard functions, even if they are not objects.
@Redchards
Redchards / Allocator.hxx
Last active January 3, 2016 15:50
Extremly simple implementation of the Arena (fixed memory pool) allocator in C++14.
#ifndef ALLOCATOR
#define ALLOCATOR
#include <MetaMinimal.hxx>
#include <cstdlib>
#include <limits>
#include <type_traits>
#include <utility>
@Redchards
Redchards / ObserverPtr.hxx
Created December 22, 2015 21:42
"Dumb" smart ptr inspired by the draft n3514 (C++14).
#ifndef OBSERVER_PTR_HXX
#define OBSERVER_PTR_HXX
#include <functional>
#include <type_traits>
/* Basically only for "void_t". You might instead want to define it yourself as this is extremly simple :
template<class ...>
using void_t = void;
@Redchards
Redchards / Range.hxx
Created December 20, 2015 12:38
Extremly simple range classes, inspired by Eric Niebler's work.
// #include <Meta.hxx>
// Normally I use my little metaprogramming class, but it should be easy to adapt the code to use something better
// (or maybe standard C++ when it got more metaprogramming features)
// If you don't care, you can grab my little library here
// https://gist.github.com/Redchards/c7a454adb151e79f4e9d
template<class T>
static constexpr bool is_empty_and_trivial = std::is_empty<T>::value && std::is_trivial<T>::value;
template<class T>
@Redchards
Redchards / Meta.hxx
Last active January 1, 2016 17:33
Simple and dirty metaprogramming library (C++14)
namespace meta
{
template<class T>
using invoke = typename T::type;
template<class T>
struct always
{
template<class ...>
@Redchards
Redchards / CallableTraits.hxx
Last active January 8, 2016 07:46
A callable traits (lambda, std::function, functions, operator() ...) based on what I remember from an implementation on the net that I can't find anymore, along with a "is_callable" metafunction.
#ifndef IS_CALLABLE_HXX
#define IS_CALLABLE_HXX
#include <functional>
#include <tuple>
template<class ...>
using void_t = void;
// In fact, operator() is defined for the standard functions, even if they are not objects.
@Redchards
Redchards / CTTI.hxx
Created December 17, 2015 21:06
Straightforward CTTI (compile time type information) implementation, based on the Eric Niebler's one, using my string classes (C++14)
namespace CTTI
{
template<class T>
static const char* GetTypeName()
{
static constexpr size_t typeNameLength = sizeof(FUNCTION) - (exprBegin.size() + exprEnd.size());
static constexpr ConstString functionName = FUNCTION;
static StackString<typeNameLength + 1> typeName(functionName.data() + exprBegin.size(), typeNameLength);
return typeName;
@Redchards
Redchards / StaticIf.hxx
Last active December 17, 2015 18:58
A very simple implementation of "static_if" in C++14, implemented in roughly 30min, inspired by the n3329 working draft specification.
// Inspired by http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3329.pdf
// Unfortunatly, there is not way to implement the most interesting feature right now : the static if inside classes.
// I didn't find a way to do this without ugly macro hacks, so we need to deal with it ...
// Oh, and special mention to SuperV1234 (https://github.com/SuperV1234) who did this far before me, with roughly the same implementation,
// a friend pointed this out after I finished implenting it (mine is slightly simpler though).
#include <iostream>
#include <type_traits>
namespace details
@Redchards
Redchards / TokenizerTst.hxx
Created December 16, 2015 23:56
Some tests with tokenizers. Much faster to C++98 versions, due to move semantic.
// These implementations are ridiculous ... should have something more sensical, like gsl::string_span.
// Only for testing purpose !!
struct default_string_span
{
using iterator = std::string::const_iterator;
default_string_span(iterator first, iterator last)
: first(first),
last(last){}
@Redchards
Redchards / ForEachArgs.hxx
Last active December 15, 2015 16:39
Apply a function to each arguments abusing C++17 upcoming fold expressions and operator coma (compiling with Clang 3.6 and above)
#include <initializer_list>
// The original version, by Eric Niebler and Sean Parent. Abusing brace initialization to make a liste expansion.
// Would work with any variadic initializable construct, such as array types (T[])
template<class Fn, class ... Args>
constexpr Fn for_each_args(Fn f, Args&& ... args)
{
using expander = std::initializer_list<int>;
return ((void)expander{(f(args), 0)...}, f);