Skip to content

Instantly share code, notes, and snippets.

@gatchamix
gatchamix / is_constexpr.cpp
Last active August 27, 2021 06:25
is_constexpr macro to determine whether a given expression can be assigned to a constexpr variable or not
#include <utility>
#include <string>
#include <string_view>
using namespace std::literals;
//
template <class F, class... Args,
class = decltype(std::declval<F&&>()(std::declval<Args&&>()...))>
@gatchamix
gatchamix / cows_bulls.cpp
Created November 17, 2017 18:48
Implementation of Cows and Bulls game
#include <cstdint>
#include <algorithm>
#include <tuple>
template <class... Ts>
auto constexpr persist(Ts volatile... ts)
{ std::tuple{ ts... }; }
auto constexpr hamming_weight(std::uint16_t const mask,
std::uint16_t const value)
@gatchamix
gatchamix / is_constexpr.cpp
Created December 12, 2017 10:43
Improved is_constexpr checking macro (by quicknir)
#include <utility>
#include <string>
#include <string_view>
using namespace std::literals;
//
auto constexpr checker(...) {}
@gatchamix
gatchamix / auto_assert.cpp
Created July 20, 2018 16:14
macro to automatically select 'static_assert' where possible, default to 'assert' otherwise
#include <cassert>
#define auto_assert(...) \
[](auto expression) \
{ \
if constexpr (noexcept(expression())) \
{ static_assert(expression()); } \
else \
{ assert(expression()); } \
} \
@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
{
@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 / 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 / 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 / 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 / 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
{};
//