Skip to content

Instantly share code, notes, and snippets.

@talybin
talybin / ct_value_index_only.cpp
Last active November 25, 2019 02:08
Get index by run-time value.
#include <iostream>
#include <utility>
template <size_t I, class F>
inline auto ct_value(F&& f)
{
return std::forward<F>(f)(std::integral_constant<size_t, I>());
}
template <class F, size_t... I>
@talybin
talybin / seq_tuple.cpp
Last active December 20, 2019 22:29
Sequence order tuple implementation (e.g order not inverted)
#include <iostream>
#include <iomanip>
#include <tuple>
// TODO fix alignment to match struct alignment
// Empty list
template <class...>
struct seq_tuple
{ };
@talybin
talybin / ct_interface.cpp
Created October 26, 2019 21:16
Nicer compile time interface (CppCon 2019, Kris Jusiak)
#include <boost/mp11/algorithm.hpp>
namespace mp11 = boost::mp11;
template<class... Ts> constexpr mp11::mp_list<Ts...> list{};
constexpr mp11::mp_quote<mp11::mp_unique> unique{};
template<class T> struct transform_ { template<class... Ts> using fn = mp11::mp_transform<T::template fn, Ts...>; };
template<class T> constexpr transform_<T> transform{};
@talybin
talybin / to_tuple.cpp
Created October 26, 2019 20:45
Struct to tuple conversion (CppCon 2019, Kris Jusiak)
#include <tuple>
#include <type_traits>
template <class T, class... TArgs>
decltype(void(T{std::declval<TArgs>()...}), std::true_type{})
test_is_braces_constructible(int);
template <class, class...>
std::false_type
test_is_braces_constructible(...);
@talybin
talybin / filter_args.hpp
Last active September 23, 2019 23:26
Alternative to argument filtering without using std::tuple_cat.
#pragma once
#include <tuple>
#include <functional>
// Remove indexes from std::index_sequence that
// do not satisfy condition.
template <template <size_t> class Cond, class Src, size_t... Fs>
struct filter_index_sequence {
using type = std::index_sequence<Fs...>;
@talybin
talybin / promise.cpp
Last active November 21, 2019 00:25
A scetch. Needs optimization.
#include <iostream>
#include <functional>
#include <tuple>
#include <type_traits>
template <class T>
struct fn_signature
: fn_signature<decltype(std::function(std::declval<T>()))>
{ };
@talybin
talybin / ard-variant.hpp
Last active September 9, 2021 19:05
Variant implementation for Arduino (C++14, no exceptions) that not using union.
// Adapted to Arduino devices (running C++14 with RTTI and exceptions disabled).
// Vladimir Talybin (2021)
//
// Note! Since Arduino has exceptions disabled the implementation become much
// simpler. All noexcept flags removed and no actions (in case exception throws)
// for invalid state applied.
// In case of invalid operation (like getting value of valueless variant) the
// program will abort (call std::abort()).
//
// Removed:
@talybin
talybin / basic_tuple_impl.hpp
Last active October 21, 2019 14:01
Basic implementation of tuple
#pragma once
#include <utility>
// Basic implementation of tuple
template <class...>
struct tuple;
template <>
struct tuple<> { };
@talybin
talybin / any_function.hpp
Last active November 21, 2019 00:28
An attempt to any function holder type with only requirement to simillar return type. Has a lot of issues. Do not use!
#pragma once
#include <functional>
#include <any>
template <class T>
struct fn_signature
: fn_signature<decltype(std::function(std::declval<T>()))>
{ };
template <class T>
@talybin
talybin / fn_signature.hpp
Last active June 7, 2019 04:42
Function signature of std::function or its callable target
// Function signature of std::function or its callable target.
// Works with gcc only, clang has no deduction guides for
// std::function in its library (tested with clang 8.0).
// However using -stdlib=libstdc++ should work.
// https://godbolt.org/z/khLhes
#pragma once
#include <functional>
template <class T>