Skip to content

Instantly share code, notes, and snippets.

View notfoundry's full-sized avatar

Mark Johnson notfoundry

  • Canada
View GitHub Profile
@notfoundry
notfoundry / example.hpp
Created October 6, 2020 21:05
Lift heterogenous eager C++ metafunction
#include "lift.hpp"
constexpr auto make_array = META_LIFT(std::array, (class, int));
using array_of_5_ints = decltype(make_array.template operator()<int, 5>());
#define PPTS_BIT_BASE 32
#define PPTS_TOKEN_l (l)
#define PPTS_TOKEN_o (o)
#define PPTS_TOKEN_n (n)
#define PPTS_TOKEN_g (g)
#define ORDER_PP_DEF_1ppts_choose_type_for_width \
ORDER_PP_FN(8fn(8N, 8print(8(_ExtInt) 8lparen 8to_lit(8mul(PPTS_BIT_BASE, 8N)) 8rparen)))
@notfoundry
notfoundry / example.hpp
Last active October 6, 2020 19:32
Universal type-level `apply` metafunction
#include <type_traits
#include "meta_invoke.hpp"
#define ASSERT_SAME(...) static_assert(std::is_same_v<__VA_ARGS__>)
struct f1 {
template <class T>
using f = T*;
};
ASSERT_SAME(META_INVOKE((f1), int), int*);
@notfoundry
notfoundry / wtf.c
Created September 9, 2020 03:05
4-bit ALU metaprogrammed in C11's _Generic
const static struct _0 {} _0;
const static struct _1 {} _1;
#define G7_SPLICE(expr) __typeof__(expr)
#define G7_STRUCT(...) (struct { __VA_ARGS__ } ){}
#define G7_NAND(a, b) \
@notfoundry
notfoundry / addition.h
Last active August 9, 2020 17:42
Brainfuck interpreter written in the C preprocessor
#include "bf.h"
#define LHS 2
#define RHS 5
PPFUCK (
(LHS, RHS),
G // G ("get") is ,
R G // R ("right") is >
( // ( is [
@notfoundry
notfoundry / meta-lambdas.hpp
Last active February 19, 2020 16:36
Type-level lambda expressions for template metaprogramming in C++20
#include <boost/preprocessor/punctuation/is_begin_parens.hpp>
#include <boost/preprocessor/punctuation/remove_parens.hpp>
#include <boost/preprocessor/control/if.hpp>
#include <boost/preprocessor/seq/for_each.hpp>
#include <boost/preprocessor/seq/variadic_seq_to_seq.hpp>
namespace metalambda {
template <auto T>
@notfoundry
notfoundry / ct_string.hpp
Created July 20, 2019 02:16
cpp_constexpr_string_interpolation
#include <boost/mp11/map.hpp>
#include <boost/mp11/list.hpp>
namespace ct {
using namespace boost::mp11;
template <class C, C... Cs>
struct nttp_string {
constexpr static C data[sizeof...(Cs)] = {Cs...};
@notfoundry
notfoundry / cpp_raw_string_align_udl.cpp
Created June 24, 2019 01:33
Align raw string literals to a common baseline at compiletime
#include <cstdlib>
#include <array>
#include <boost/mp11/list.hpp>
#include <boost/mp11/algorithm.hpp>
using namespace boost::mp11;
template <class C, C... Cs>
struct nttp_string {};
#include <utility>
template <std::size_t Index, class T>
struct indexed_pack_element {
constexpr static T element(std::integral_constant<std::size_t, Index>);
};
template <class IndexSeq, class... Ts>
struct pack_indices_impl;
@notfoundry
notfoundry / static_typeid.hpp
Last active November 24, 2018 19:05
static_typeid
#include <cstddef>
namespace nonstd {
namespace detail {
template <class Tag, class Type = int, Type Start = 0, Type Step = 1>
class meta_counter {
template <Type N>
struct flag {
friend constexpr bool adl_flag (flag<N>);
};