Skip to content

Instantly share code, notes, and snippets.

@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
#pragma once
#include <cstdint>
#include <string_view>
namespace fnv1b {
/* FNV-1b hash function with extra rotation
* https://en.wikipedia.org/wiki/Fowler–Noll–Vo_hash_function
*/
template<typename T = std::size_t>
inline T hash(const char* str, std::size_t size) noexcept;
@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>
@hutorny
hutorny / print_iterable.hpp
Created November 15, 2018 08:19
A c++ template to print any iterable
#include <iterator>
#include <utility>
// see live example on http://coliru.stacked-crooked.com/a/591f4db5a008cb5a
template<class Stream, class Vector, class Begin = decltype(std::begin(std::declval<Vector>()))>
inline Stream& operator<<(Stream& stream, const Vector& vect) {
const char* dlm = "";
for(const auto& i : vect) { stream << dlm << i; dlm = ", "; }
return stream;
}