Skip to content

Instantly share code, notes, and snippets.

// Merge using same set of arguments for construction
template <class... Ts>
struct overload : Ts... {
template <class... Args>
overload(Args&&... args) : Ts{ std::forward<Args>(args)... }... { }
};
template <class... Args>
auto make_reversed_tuple(Args&&... args) {
return []<class T, size_t... I>(T&& t, std::index_sequence<I...>) {
return std::forward_as_tuple(std::get<sizeof...(I)-1-I>(std::forward<T>(t))...);
}
(std::forward_as_tuple(std::forward<Args>(args)...), std::index_sequence_for<Args...>());
}
template <class T, class Type, class T1, class... Args>
decltype(auto) reversed_invoke(Type T::* f, T1&& t1, Args&&... args)
{
return [f]<class Tup, size_t... I>(T1&& arg, Tup&& t, std::index_sequence<I...>) {
return std::invoke(f, std::forward<T1>(arg),
std::get<sizeof...(I)-1-I>(std::forward<Tup>(t))...);
}
(std::forward<T1>(t1),
std::forward_as_tuple(std::forward<Args>(args)...),
std::index_sequence_for<Args...>());
// Unreference tuple for raw memory copy.
// Primary template for non referenced elements.
template <class T, class = void>
struct tuple_unref {
tuple_unref(T& t) noexcept : value(t) { }
T& value;
};
// A specialization for tuple containing
// at least one reference type.
@talybin
talybin / filter_args.hpp
Last active February 24, 2019 12:54
Filter arguments
#pragma once
#include <tuple>
// Conditionally select argument.
// On true return argument packed in std::tuple.
template <bool>
struct zero_or_one {
template <class T> static std::tuple<T&&> get(T&& t) {
return std::forward<T>(t);
}
@talybin
talybin / stream_exception.hpp
Last active March 8, 2019 12:11
Stream to exception before throw
#include <sstream>
struct stream_exception : std::exception
{
using std::exception::exception;
template <class T>
stream_exception& operator<<(const T& arg) {
std::ostringstream oss;
oss << arg;
@talybin
talybin / any_string_acceptor.hpp
Created March 14, 2019 10:51
Qt: Accept any string type
// Usage:
// void fn(const string_type& str) {
// std::cout << str << '\n';
// }
struct string_type : std::string_view
{
// Takes const char*, std::string, std::string_view
template <class T, class = std::enable_if_t<
std::is_constructible_v<std::string_view, const T&>
>>
@talybin
talybin / function.hpp
Last active March 27, 2019 23:23
Simplified implementation of std::function
#pragma once
template <class>
struct function;
template <class R, class... Args>
struct function<R(Args...)> {
private:
template <class F>
static R call_impl(void* fn, Args&&... args) {
// Extract function argument types
#pragma once
#include <type_traits>
// std::void_t since C++17
template <class...> using void_t = void;
// Primary template
template <class F, class = void>
@talybin
talybin / map_the_type.cpp
Created April 25, 2019 06:45
Runtime mapping to a type
#include <iostream>
#include <variant>
#include <map>
struct diagram {
diagram() { std::cout << "diagram constructed\n"; }
};
struct unit {
unit() { std::cout << "unit constructed\n"; }