Skip to content

Instantly share code, notes, and snippets.

View NocturnDragon's full-sized avatar

X NocturnDragon

View GitHub Profile
char * stringify~for_int (int x);
char * stringify~for_char (char c);
// The compiler can infer the suffix to use
stringify (1); // Same as stringify~for_int (1)
char * (*ptr)(int x) = stringify~for_int (1); // It can be given explicitly to get an exact function
//SOURCE: https://www.reddit.com/r/cpp/comments/9wjfi6/c2x_the_next_real_revision_of_the_c_standard/e9lnq00/
@NocturnDragon
NocturnDragon / EnumName.cpp
Created October 25, 2018 16:25
A supremely dirty way of converting an enum value to a string at runtime, without the use of double-declaration XMacros or its ilk.
// Source: https://twitter.com/zygoloid/status/735275127873540096
//
// A supremely dirty way of converting an enum value to a string at runtime, without
// the use of double-declaration XMacros or its ilk.
//
// Very silly code but it actually works.
//
#include <stdio.h>
@NocturnDragon
NocturnDragon / local_opaque_object.c
Created July 18, 2018 15:41 — forked from keebus/local_opaque_object.c
Local opaque object in C
// library.h -------------------------------------------------------------------
#define opaque(Type) Type; int $sizeof_##Type(void);
#define opaque_impl(Type) int $sizeof_##Type(void) { return sizeof(Type); }
#define local(Type) ((Type *)alloca($sizeof_##Type()))
// foo.h -----------------------------------------------------------------------
@NocturnDragon
NocturnDragon / templated_functions.cpp
Last active May 4, 2018 23:27
Better templated functions on values syntax
// I wish I could write
int foo(int bar, constexpr bool add)
{
if constexpr(add)
{
return bar + 1;
}
return 5;
}
@NocturnDragon
NocturnDragon / Doc.md
Created January 10, 2018 20:01 — forked from dwilliamson/Doc.md
Minimal Code Generation for STL-like Containers

This is my little Christmas-break experiment trying to (among other things) reduce the amount of generated code for containers.

THIS CODE WILL CONTAIN BUGS AND IS ONLY PRESENTED AS AN EXAMPLE.

The C++ STL is still an undesirable library for many reasons I have extolled in the past. But it's also a good library. Demons lie in this here debate and I have no interest in revisiting it right now.

The goals that I have achieved with this approach are:

@NocturnDragon
NocturnDragon / with.cpp
Last active October 3, 2017 14:47
With constructor in C++17
#include <cstdio>
#define with(a) if(a ; true)
struct ExampleScopeGuard
{
ExampleScopeGuard () : foo(5)
{
printf("ACQUIRE\n");
}
@NocturnDragon
NocturnDragon / Pimpl.cpp
Created April 17, 2017 16:58 — forked from dwilliamson/Pimpl.cpp
How I Pimpl
struct Data
{
Data()
{
}
~Data()
{
}
// File: pjson.h - written by Rich Geldreich 2012 - License: Unlicense http://unlicense.org/
#ifndef PURPLE_JSON_H
#define PURPLE_JSON_H
#ifdef WIN32
#pragma once
#endif
#include <string>
#include <vector>
@NocturnDragon
NocturnDragon / Jinja module loader.md
Created February 15, 2017 22:45 — forked from voscausa/Jinja module loader.md
Jinja2 compiled templates module loader for App Engine Pyhton 2.7.

Jinja compiled templates module loader

This code is part of a Jinja CMS for Google App Engine Python 2.7 and NDB datastore

A Jinja enviroment is created for every CMS site: site_key_id = 'example

The modules are created using compiler.py The resulting code objects are stored in the dadastore using Kind Runtimes and a BlobProperty

The modules can also be saved / downloaded as .pyc in a zip archive: -compiled-templates.zip

@NocturnDragon
NocturnDragon / ClCrash.cpp
Created October 7, 2016 13:59
Cl Internal Compiler Error
// Command line a simple
// cl ClCrash.cpp
struct foo
{
float x;
float y;
__declspec(property(get = get_x)) foo xx;