Skip to content

Instantly share code, notes, and snippets.

#include <variant>
#include <type_traits>
#include <iostream>
#include <concepts>
#include <print>
//
#define FWD(...) static_cast<decltype(__VA_ARGS__)&&>(__VA_ARGS__)
@gatchamix
gatchamix / defer member func.cpp
Last active January 22, 2019 08:57
simplifies the creation of non-const member functions that defer to their const member function counterparts
#include <type_traits>
//
template <class T>
struct remove_qualifiers
: std::remove_cv<T>
{};
template <class T>
@gatchamix
gatchamix / static_polymorphism_advanced.cpp
Created October 12, 2018 10:56
additional compile-time replacements for polymorphism (more intrusive for end-user)
#include <type_traits>
//
template <class...>
struct type_pack
{};
//
@gatchamix
gatchamix / static_polymorphism.cpp
Created October 12, 2018 10:32
simulating features of dynamic polymorphism and class inheritance at compile-time
#include <type_traits>
//
template <class...>
struct type_pack
{};
//
@gatchamix
gatchamix / has_member_function.cpp
Last active October 12, 2018 09:06
faux-function to determine whether a class/struct has a function (optionally checking for a specific type signature)
#include <type_traits>
//
template <class...>
struct type_pack
{};
//
@gatchamix
gatchamix / is_constexpr.cpp
Created September 18, 2018 10:13
solutions for determining if an expression meets the standard's definition of constant-expression
#include <utility>
#include <string>
#include <string_view>
#include <type_traits>
using namespace std::literals;
#define version 17
// C++11 solution
@gatchamix
gatchamix / insertion_sort.cpp
Created August 15, 2018 23:34
fast insertion sort for non-type template parameter packs in C++2a
static auto constexpr less = [](auto&& l, auto&& r) { return l < r; };
static auto constexpr less_equal = [](auto&& l, auto&& r) { return l <= r; };
static auto constexpr greater = [](auto&& l, auto&& r) { return l > r; };
static auto constexpr greater_equal = [](auto&& l, auto&& r) { return l <= r; };
//
template <template <auto...> class C, auto... Vs, class Fn>
auto constexpr sort(C<Vs...> in, Fn)
{
@gatchamix
gatchamix / constant.cpp
Created July 28, 2018 02:44
compile-time constant wrapper
#include <cstddef>
#include <type_traits>
//
template <auto V>
struct constant
{
using value_type = decltype(V);
@gatchamix
gatchamix / tuple2a.cpp
Last active July 24, 2019 15:55
simple tuple container using C++2a
#include <utility>
#include <type_traits>
//
namespace detail
{
template <auto I, class T>
struct indexer_elem
{};
@gatchamix
gatchamix / matrix17.cpp
Last active July 25, 2018 21:14
simple matrix container using C++17 (original idea courtesy of jdmarsh)
#include <utility>
#include <cstddef>
#include <array>
#include <type_traits>
//
template <class T, std::size_t Rows, std::size_t Cols = Rows>
struct matrix
{