Skip to content

Instantly share code, notes, and snippets.

@Eczbek
Eczbek / meta_math.h
Last active June 21, 2026 21:24
generic math
#ifndef DETAIL_META_HEADER_MATH
#define DETAIL_META_HEADER_MATH
#include <stddef.h>
#define meta_int_cmp(X, ...) \
meta_int_cmp(sizeof(X), sizeof(__VA_ARGS__), (typeof(X))-1 < 0, (typeof(__VA_ARGS__))-1 < 0, (unsigned char*)&(typeof(X)){ (X) }, (unsigned char*)&(typeof(__VA_ARGS__)){ (__VA_ARGS__) })
[[maybe_unused]] static inline int (meta_int_cmp)(size_t a_size, size_t b_size, bool a_signed, bool b_signed, unsigned char* a_data, unsigned char* b_data) {
static constexpr int sign_mask = ~((unsigned char)-1 >> 1);
bool a_sign = a_signed && (a_data[a_size - 1] & sign_mask);
@Eczbek
Eczbek / meta_map.h
Created June 18, 2026 05:44
generic hashmap
#ifndef DETAIL_META_HEADER_MAP
#define DETAIL_META_HEADER_MAP
#include <stdbit.h>
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#define meta_map_header(MAP) \
(*(union meta_map_header*)(MAP))
@Eczbek
Eczbek / meta_swap.h
Last active June 7, 2026 02:20
generic-ish swap
#ifndef DETAIL_META_HEADER_SWAP
#define DETAIL_META_HEADER_SWAP
#include <stddef.h>
#include <string.h>
// Assigns each variable to the other's value.
// If the variables have different types, each is cast to the other's type before assignment.
#define meta_swap(A, B) \
((void)_Generic(typeof_unqual(A), typeof_unqual(B): meta_swap(true, &(A), &(B), &(char[sizeof(A)]){}, sizeof(A)), default: _Generic(typeof_unqual(A), typeof_unqual(B): (int){}, default: (A)) = (typeof(_Generic(typeof_unqual(A), typeof_unqual(B): 0, default: (A)))){ (typeof(_Generic(typeof_unqual(A), typeof_unqual(B): 0, default: (A))))*(typeof(_Generic(typeof_unqual(A), typeof_unqual(B): 0, default: (B)))*)meta_swap(false, &(B), &(typeof(_Generic(typeof_unqual(A), typeof_unqual(B): 0, default: (B)))){ (typeof(_Generic(typeof_unqual(A), typeof_unqual(B): 0, default: (B))))(_Generic(typeof_unqual(A), typeof_unqual(B): 0, default: (A))) }, &(char[sizeof(B)]){}, sizeof(B)) }))
@Eczbek
Eczbek / meta_array.h
Last active June 4, 2026 17:50
dynamic array
#ifndef DETAIL_META_HEADER_ARRAY
#define DETAIL_META_HEADER_ARRAY
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#define meta_array_header(ARRAY) \
((meta_array_header*)(ARRAY) - 1)
typedef struct {
@Eczbek
Eczbek / meta_print.h
Last active June 15, 2026 20:15
type-safe print
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#define DETAIL_EVAL(...) DETAIL_EVAL0(DETAIL_EVAL0(DETAIL_EVAL0(DETAIL_EVAL0(__VA_ARGS__))))
#define DETAIL_EVAL0(...) DETAIL_EVAL1(DETAIL_EVAL1(DETAIL_EVAL1(DETAIL_EVAL1(__VA_ARGS__))))
#define DETAIL_EVAL1(...) DETAIL_EVAL2(DETAIL_EVAL2(DETAIL_EVAL2(DETAIL_EVAL2(__VA_ARGS__))))
#define DETAIL_EVAL2(...) DETAIL_EVAL3(DETAIL_EVAL3(DETAIL_EVAL3(DETAIL_EVAL3(__VA_ARGS__))))
#define DETAIL_EVAL3(...) __VA_ARGS__
// Evaluates to whether the arguments' types are compatible.
// Bounded array types are considered incompatible with unbounded array types.
#define meta_is_same_type(X, ...) _Generic(typeof(X),typeof(__VA_ARGS__):meta_is_unbounded_array_type(X)==meta_is_unbounded_array_type(__VA_ARGS__),default:0)
// Evaluates to whether the arguments' types are compatible, ignoring qualifiers.
// Bounded array types are considered incompatible with unbounded array types.
#define meta_is_same_type_unqual(X, ...) _Generic(typeof_unqual(X),typeof_unqual(__VA_ARGS__):meta_is_unbounded_array_type(X)==meta_is_unbounded_array_type(__VA_ARGS__),default:0)
// Accepts a condition expression and two other expressions.
// The condition must be constant and integer-like.
@Eczbek
Eczbek / as.hpp
Last active June 19, 2026 18:24
as
#include<concepts>
struct as{template<class T>operator T(){throw;};template<class T>struct x{T x;template<class U>[[nodiscard]]constexpr decltype(static_cast<U>((T)x))operator->*(U(as::*)())noexcept(noexcept(static_cast<U>((T)x))){return static_cast<U>((T)x);}};};constexpr auto operator->*(auto&&x,std::same_as<as>auto)noexcept{return as::x<decltype(x)&&>((decltype(x))x);}
#define as ->*::as()->*&::as::operator
@Eczbek
Eczbek / lambdas.md
Last active September 12, 2025 02:14
article on lambdas

Lambdas

A lambda expression is a shorthand notation for creating an unnamed callable object (also called a closure, or an anonymous function). A lambda can "capture" variables from its surrounding scope by value or by reference, allowing the body of the lambda to access or modify those variables without having to pass them as parameters. Unlike regular functions, lambdas are typically written in-line, combining the reusability of functions with direct access to local context. The lambda retains captured variables' state (for captures by value) or dynamically references them (for captures by reference), making lambdas ideal for short, context-dependent operations like custom comparisons, filters, or event handlers.

Example:

bool is_even(int x) {
	return x % 2 == 0;
}

int main() {
std::string vtable_inator(const auto& any) {
struct h { void** name; };
return std::string(reinterpret_cast<char*>(reinterpret_cast<void**>(reinterpret_cast<const h*>(&any)->name[-1])[1]));
}
struct vtable_inator_able { virtual void nothing() final {}; };
struct Hello_world : vtable_inator_able {};
int main() {
const a=-3/80,e=1,f=5,g=8/3,h=9/10,i=1,l=11/3,n=3,o=1/3,r=1,s=7/3,t=10/3,u=12/5,v=1,w=9/5,x=18/7,z=0;
z*e*r*o // 0
o*n*e // 1
t*w*o // 2
t*h*r*e*e // 3
f*o*u*r // 4
f*i*v*e // 5
s*i*x // 6
s*e*v*e*n // 7