Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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>
#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>>
#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;
/* constexpr any_of against a list of constants */
/** type_of_list<...>::type - helper structure for determining type of the first item in the list */
template<auto ... List> struct type_of_list;
template<auto List> struct type_of_list<List> { using type = decltype(List); };
template<auto First, auto ... List > struct type_of_list<First, List...> : type_of_list<First> {};
/** type_of<...>::type - determines type of the first item in the list */
template<auto ... List>
using type_of = typename type_of_list<List...>::type;
@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;
}
@hutorny
hutorny / anotherclass.ccs
Created April 4, 2017 11:11
Cascaded Configuration Sets for C++1y
/************************************************************************/
/* anotherclass.ccs definition of configuration options and sets for */
/* MyAnotherClass */
/************************************************************************/
#include <ccs>
#pragma once
class MyAnotherClass;
namespace configuration {
template<>
struct Configuration<MyAnotherClass, Default> {
@hutorny
hutorny / miculog.config
Created March 24, 2017 09:47
Minimalistic C++11 logger with per-user-class configuration
/**
* This file is intended for miculog per-class configuration
*/
/* remove this message from your own copy of miculog.config */
#pragma message "Default miculog configuration is in use"
/* add forward declaration of your classes here,
* embrace classes in namespaces, as needed