Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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
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 / to_list.cpp
Created October 13, 2017 13:57
helper macro to convert applicable data into an auto_list equivalent
#include <utility>
#include <iterator>
#include <array>
#include <string_view>
//
template <auto...>
struct auto_list
{};
@gatchamix
gatchamix / static_class_2a.cpp
Created September 7, 2017 16:07
improvement to static class design using hana::is_valid + C++2a explicit lambda templates
#include <utility>
//
template <class F, class... Args,
class = decltype(std::declval<F&&>()(std::declval<Args&&>()...))>
auto constexpr is_valid_impl(int)
{ return true; }
template <class F, class... Args>
@gatchamix
gatchamix / tuple17.cpp
Last active July 24, 2018 13:35
simple tuple container using C++17
template <std::size_t N, class T>
struct tuple_elem
{
constexpr tuple_elem(T&& val)
: val_{ std::forward<T>(val) }
{}
template <auto I, std::enable_if_t<I == N>* = nullptr>
auto constexpr& operator[](constant<I>)
{ return val_; }
@gatchamix
gatchamix / meta17.cpp
Last active August 31, 2017 21:21
meta-programming types and algorithms using C++17/2a
#include <type_traits>
#include <utility>
#include <cstddef>
#include <string_view>
#include <iterator>
#include <limits>
#include <tuple>
//
@gatchamix
gatchamix / template_list.cpp
Last active August 27, 2017 16:59
template type and non-type list and manipulation functions (type_t/auto_t)
#include <type_traits>
#include <limits>
#include <utility>
//
template <class... Ts>
struct type_t
{ static auto constexpr size = sizeof...(Ts); };
@gatchamix
gatchamix / constexpr_switch.cpp
Last active October 11, 2021 10:58
constexpr switch/case/default
#include <type_traits>
#include <utility>
#include <limits>
#include <array>
//
template <auto V_>
struct auto_t
{