Skip to content

Instantly share code, notes, and snippets.

View ecatmur's full-sized avatar
👻

Ed Catmur ecatmur

👻
  • Maven Securities
  • Chicago
View GitHub Profile
#include <boost/preprocessor.hpp>
#include <iostream>
template<int r> void print(int i) {
if constexpr (r)
if constexpr (r % 5)
if constexpr (r % 3)
std::cout << i << std::endl;
else
@ecatmur
ecatmur / irony.cpp
Last active June 11, 2020 13:16
Hardened Y combinator that handles recursive return type deduction via [dcl.spec.auto]/10
#include <utility>
#include <type_traits>
template <class F>
struct fix_t {
template <class Ff>
struct ref {
Ff ff;
template <class... Args>
constexpr decltype(auto) operator()(Args&&... args) const {
@ecatmur
ecatmur / any-exception.md
Last active February 4, 2019 08:03
AnyException

Here's a fairly simple program. Can you see the bug? (Aside from using functions with wide contracts, which is a matter of opinion.)

That's right, we failed to invoke any cleanup. That's because when the exception handling mechanism cannot find a handler for a thrown exception, it's implementation-specified whether the stack is unwound. So, if we want to invoke destructors and shut down the program cleanly, we need to wrap main() in a try-catch block.

Oh dear. Now we've managed to invoke destructors and shutdown cleanly, we've lost a heap of useful information about the exception - where previously we had a backtrace and a core file, now all we've got is the type and the explanatory string (what()).

@ecatmur
ecatmur / array_concat.cpp
Last active June 30, 2017 15:44
Non-recursive variadic array concat using indexing arrays and folds.
#include <array>
#include <type_traits>
#include <utility>
template<class> struct Nth;
template<std::size_t... Is> struct Nth<std::index_sequence<Is...>>
{
template<std::size_t> struct Ignore { template<class T> constexpr Ignore(T&&) {} };
template<class T, class... Us> constexpr T&& operator()(Ignore<Is>..., T&& t, Us&&...) { return std::forward<T>(t); }
};
// Based on https://www.reddit.com/r/cpp/comments/4yp7fv/c17_structured_bindings_convert_struct_to_a_tuple/
#include <boost/preprocessor/repetition/repeat.hpp>
#include <tuple>
#include <type_traits>
#include <cassert>
struct any_type { template<class T> constexpr operator T(); };
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-field-initializers"
#include <boost/preprocessor/control/if.hpp>
#include <boost/preprocessor/punctuation/comma_if.hpp>
#include <boost/preprocessor/repetition/repeat.hpp>
#include <tuple>
#include <type_traits>
namespace impl {
#define SWITCH_MAX 64
#include <cstring>
#include <functional>
#include <iostream>
#include <new>
#include <utility>
template<std::size_t Size>
class TrivialBuffer
{
public:
#include <functional>
#include <iostream>
#include <new>
#include <utility>
struct SmallBufferTable
{
void (*destructor)(void const*);
void (*copier)(void*, void const*);
void (*mover)(void*, void*);
// Pure Z combinator in C++14 variadic polymorphic lambdas.
auto fix = [](auto&& f) {
return [&](auto&& x) {
return x(x); }([f = static_cast<decltype(f)&&>(f)](auto& x) mutable {
return [&](auto&&... a) -> decltype(auto) {
return f([&](auto&&... b) -> decltype(auto) {
return x(x)(static_cast<decltype(b)&&>(b)...); },
static_cast<decltype(a)&&>(a)...); }; }); };
#include <vector>
#include <iostream>
#include <iomanip>
struct Row {
int off;
std::vector<double> buf{};
double b = -4.;
};