Skip to content

Instantly share code, notes, and snippets.

@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()>'
@talybin
talybin / waterfall.cpp
Last active August 19, 2021 13:19
List of depended functions
#include <iostream>
template <class F>
struct waterfall
{
waterfall(F&& f)
: fn(std::forward<F>(f))
{}
template <class... Args>
@talybin
talybin / tuple_add.cpp
Created August 24, 2020 09:48
tuple_cat
#include <tuple>
#include <string>
template <class>
struct is_tuple : std::false_type
{ };
template <class... T>
struct is_tuple<std::tuple<T...>> : std::true_type
{ };
@talybin
talybin / typeid_name.cpp
Last active August 4, 2021 22:15
Demagled typeid::name CT alternative (reflection).
#include <iostream>
#include <string_view>
template <class T>
constexpr std::string_view typeid_name(const T&)
{
std::string_view sv = __PRETTY_FUNCTION__;
sv.remove_prefix(sv.find('=') + 2);
return { sv.data(), sv.find(';') };
@talybin
talybin / dyn_array.cpp
Created December 10, 2019 22:04
Dynamic array without allocation.
#include <iostream>
#include <string_view>
struct header
{
header* prev;
std::string_view name;
std::string_view value;
header(header* other = nullptr) noexcept
@talybin
talybin / sean_parent-cppcon.cpp
Created November 30, 2019 01:45
Create xml doc with undo feature (Sean Parent @CppCon)
#include <iostream>
#include <vector>
#include <memory>
#include <cassert>
template <class T>
void draw(const T& x, std::ostream& out, size_t position)
{
out << std::string(position, ' ') << x << std::endl;
@talybin
talybin / c_function_lamda_capture.cpp
Created November 30, 2019 01:43
Pass lambda with capture to C function
#include <iostream>
//
// Lambda without captures is convertible to function pointer.
// For a definition like this:
//
// void some_c_func(void (*callback)());
//
// We can just pass lambda to above function without captures.
//
@talybin
talybin / reduce_compile_time.cpp
Created November 28, 2019 23:32
Short research on how to reduce compile time for a large type list
#include <iostream>
#include <tuple>
// Compile with
// time g++ -std=c++17 -ftemplate-depth=1100 reduce_compile_time.cpp
// Note! -ftemplate-depth=1100 needed only if USE_TYPELIST not defined.
// There are four different methods to select type from a type list
// by run-time value.
//
@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>