Skip to content

Instantly share code, notes, and snippets.

@talybin
talybin / packed_tuple.hpp
Last active September 24, 2021 07:40
A tuple that can be aligned to any number (not just power of 2) and does not use "pragma pack"
#pragma once
#include <type_traits>
#include <tuple>
#include <memory>
template <std::size_t N, class... Types>
struct packed_tuple;
// is_packed_tuple
template <class T>
@talybin
talybin / constexpr_counter.hpp
Last active September 19, 2021 20:08
Constant expression counter
namespace detail
{
template <class T>
struct flag
{
struct dummy {
constexpr dummy() {}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wnon-template-friend"
@talybin
talybin / to_const_integral.cpp
Last active August 25, 2021 21:18
Yet another way to convert run-time integer to constant.
#include <iostream>
#include <ctime>
#include <utility>
template <class T, class F, T... I>
bool to_const(T value, F&& fn, std::integer_sequence<T, I...>) {
return (
(value == I && (fn(std::integral_constant<T, I>{}), true))
|| ... // or continue
);
@talybin
talybin / concept_lite.cpp
Created August 20, 2021 19:30
Concept Lite (Filip Roséen)
#include <type_traits>
template <class T, class = std::enable_if_t<std::is_integral<T>::value>>
using integral_t = T;
// the usage
template <class T>
void f(integral_t<T> a) {}
int main()
@talybin
talybin / type_only_function.cpp
Last active August 20, 2021 09:52
Type only function (no object required)
#include <iostream>
struct printer
{
template <class T>
constexpr void operator()(const T& t) const {
std::cout << t << '\n';
}
};
@talybin
talybin / one_arg_function.cpp
Last active August 12, 2021 07:45
Function taking any argument
#include <iostream>
#include <functional>
#include <any>
struct any_type : std::any
{
using std::any::any;
template <class T> operator T() const {
return std::any_cast<T>(*this);
}
@talybin
talybin / type_name.hpp
Last active August 18, 2021 09:37
Type name revisited. This generates only actual type of name in output binary.
#pragma once
#include <utility>
template <class T, std::size_t... I>
inline const char* type_name(std::index_sequence<I...>) {
static constexpr char name[] = { __PRETTY_FUNCTION__[I + 60]..., 0 };
return name;
}
template <class T>
@talybin
talybin / inner_struct.cpp
Last active August 5, 2021 23:48
The type of inner struct.
#include <iostream>
#include <string_view>
template <class T>
constexpr std::string_view type_name(const T&)
{
std::string_view sv = __PRETTY_FUNCTION__;
sv.remove_prefix(sv.find('=') + 2);
return { sv.data(), sv.find(';') };
@talybin
talybin / ostringstream.hpp
Last active August 25, 2021 11:50
Simple ostringstream implementation for Arduino
#pragma once
#include <string>
namespace ard
{
namespace detail
{
// Type to format mapping
template <class T> constexpr nullptr_t type_fmt {};
using fmt_t = const char[];
@talybin
talybin / lambda_decompose.cpp
Last active August 27, 2020 13:02
Lambda closure decomposition
#include <iostream>
template <class T> struct overload : T { using T::operator(); };
template <class T> overload(T) -> overload<T>;
int main()
{
auto f = [x = 1, y = 2]() { return x + y; };
// auto [a, b] = f; // error: cannot decompose lambda closure type 'main()::<lambda()>'