Skip to content

Instantly share code, notes, and snippets.

@dakka
dakka / demangle.hpp
Created September 6, 2024 23:35
C++ Demangle function, returning std::string; for Clang and GCC
#include <string>
#include <memory>
#include <cstdlib>
#if defined __clang__ || defined __GNUC__
#include <cxxabi.h>
#endif
template<typename T>
const std::string demangle() noexcept
{
@dakka
dakka / numberwords-enum.cpp
Last active August 24, 2024 20:53
Create C++ source for an enum class with named numbers. 0 based.
#include <iostream>
#include <string>
#include <string_view>
#include <vector>
#include <sstream>
#include <array>
#include <cstdlib>
constexpr std::array ones {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
constexpr std::array teens {"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
@dakka
dakka / call_once_with_result.hpp
Last active September 11, 2023 21:06
call_once_with_result - a std::call_once storing the callable object's result and making it available, C++17
/-----------------------------------------------------------------------------------------
// Copyright (C) 2022-23 Fix8 Market Technologies Pty Ltd
// by David L. Dight
//
// call_once_with_result - a std::call_once storing the callable object's result and making it available
// class once_with_result and function call_once_with_result
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
@dakka
dakka / pow_cpp20_template.cpp
Last active May 25, 2023 07:43
make_pow_array - create compiler generated constexpr C++20 arbitrary base and size power tables as std::array. Examples include uint128_t, bases 2, 3, 5, 8, 10, with constexpr printer and demangler. Uses folding, variable templates, constraints.
//-----------------------------------------------------------------------------------------
// Copyright (C) 2022-23 Fix8 Market Technologies Pty Ltd
// by David L. Dight
//
// make_pow_array - create compiler generated constexpr C++20 arbitrary base and size power
// tables as std::array. Examples include uint128_t, bases 2, 3, 5, 8, 10, with constexpr
// printer and demangler. Uses folding, variable templates, constraints.
//
// Distributed under the Boost Software License, Version 1.0 August 17th, 2003
//