Skip to content

Instantly share code, notes, and snippets.

View hexagon62's full-sized avatar
💭
Statuses are a thing?

Travis hexagon62

💭
Statuses are a thing?
View GitHub Profile
@hexagon62
hexagon62 / 1_easier_way_i_think.cpp
Last active March 29, 2020 09:23
The easy way to see if a type properly defines the == operator
#include <type_traits>
#include <functional>
template<typename T>
using eq_decltype = decltype(std::declval<T>() == std::declval<T>());
template<typename T, typename = std::void_t<>>
struct is_equality_comparable : std::false_type {};
template<typename T>
@hexagon62
hexagon62 / self_referential_function_types.hpp
Last active March 20, 2020 10:29
Self-referential function types
#include <functional>
// This class allows for a function that can return another function with the same signature
// ex: a function that takes an int and returns another function that takes an int
// Normally this would result in an infinitely recursive type, but by using implicit conversions
// we can make the compiler instead only flip-flop between 2 types as needed in a finite manner.
template<typename... Args>
class StateFuncImpl
{
public:
@hexagon62
hexagon62 / parameter_deduction.cpp
Last active October 10, 2019 08:31
Can the types of the parameters of a lambda be extracted/deduced in C++? As it turns out, yes! See the second file in the gist for how to do it.
#include <iostream>
#include <functional>
#include <tuple>
#include <string>
template<typename T>
using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;
// Let's say I have a class like this where I need to store heterogenous data
// I don't want to resort to type-erasure