Skip to content

Instantly share code, notes, and snippets.

#include <type_traits>
#if __cplusplus > 201709L
#include <bit>
template<typename T>
constexpr int countr_zero(T val) noexcept { return std::countr_zero(val); }
template<typename T>
constexpr int popcount(T val) noexcept { return std::popcount(val); }
#else
template<typename T, typename = std::enable_if_t<std::is_integral<T>::value,T>>
@hutorny
hutorny / multidispatcher.cpp
Created January 16, 2021 08:40
Multimethods for C++, example 1, calculus multidispatcher
#include <iostream>
#include <stdexcept>
#define TRACE() \
std::cout << __PRETTY_FUNCTION__ << std::endl;
namespace details {
template<class Parameter>
struct expected {
template<class Argument>
@hutorny
hutorny / multifunctions.cpp
Last active January 16, 2021 08:44
Multimethods for C++, example 2, calculus multifunction
#include <iostream>
#include <stdexcept>
#define TRACE() \
std::cout << __PRETTY_FUNCTION__ << std::endl;
namespace details {
template<class Parameter>
struct expected {
template<class Argument>
@hutorny
hutorny / multimethod.cpp
Last active January 16, 2021 09:02
Multimethods for C++, example 2, shapes multimethod
#include <iostream>
#include <stdexcept>
#define TRACE() \
std::cout << __PRETTY_FUNCTION__ << std::endl;
namespace details {
template<class Class>
struct is_virtual_parameter {
static constexpr bool value =
std::is_polymorphic_v<std::remove_reference_t<Class>> and
@hutorny
hutorny / is_constexpr.h
Created December 16, 2023 16:49
is_constexpr implementation for function returning structural type (integral or enumeration)
#include <type_traits>
// is_constexpr implementation
// Live example https://godbolt.org/z/79adTjdax
template<auto F>
struct constexpr_true : std::true_type {};
template<auto F>
static auto test_constexpr(int) -> constexpr_true<F()>;
template<auto F>