Skip to content

Instantly share code, notes, and snippets.

View RedBeard0531's full-sized avatar

Mathias Stearn RedBeard0531

View GitHub Profile
@RedBeard0531
RedBeard0531 / symmetric_coro_in_ts.cpp
Created February 5, 2019 18:07
Symmetric coroutines using the coroutines TS
// A possible implementation of symmetic coroutines on top of the asymmetric coroutines ts.
// At the bottom of this file, I've rewritten the symmetric examples from Boot.Coroutine
// using this mechanism. You can see the originals at
// https://www.boost.org/doc/libs/1_69_0/libs/coroutine/doc/html/coroutine/coroutine/symmetric.html
#include <experimental/coroutine>
#include <cassert>
#include <list>
#include <optional>
#include <vector>
template <typename T>
struct semi_lazy {
struct promise_type {
std::optional<T> val;
std::function<void()> defer;
suspend_never initial_suspend() { return {}; }
suspend_always final_suspend() { return {}; }
void return_value(T v) {
val.emplace(std::move(v));
#include <type_traits>
#include <future>
#include <functional>
template <typename T>
struct expected {
expected(const T&) {}
};
template <typename T>
struct Promise {
#include <utility>
#include <exception>
// This is probably customizable
using Error = std::exception_ptr;
template <typename R, typename T>
concept ValueReciever = requires (T a, R r) {
std::move(r).value(std::move(a));
};
@RedBeard0531
RedBeard0531 / cpp17.md
Last active September 21, 2022 03:50
New C++17 features

Supplement to this video. Watch first 35 min (the language bits), then 1:14:30 - 1:18:15 (the new map features).

You can find a fairly comprehensive list of new features on cppreference or here if you really want to see it all.

Recap of features described in the video

#include <cstdlib>
#include <boost/optional.hpp>
#include <string>
using boost::optional;
template <typename T>
struct Future{};
template <typename T>
template <typename T>
struct ValueReceiver {
virtual void value(T) = 0;
};
template <typename T>
struct ValueOrErrorReceiver : ValueReceiver<T> {
virtual void value(T) = 0;
virtual void error(std::exception_ptr) = 0;
};
#include <functional>
#include <exception>
#include <optional>
#include <memory>
#include <cassert>
#include <atomic>
#include <iostream>
#include <mutex>
#include <condition_variable>
#include <thread>
@RedBeard0531
RedBeard0531 / include_magic.md
Last active June 13, 2018 18:49
Proposal for making third-party libs easier to use

Magic #includes (and eventually imports)

By: Mathias Stearn
Please provide feedback either by email, or on #sg15-tooling in cpplang.slack.com.

This is an alternative to a proposal that I'm sure was offered with good intentions, but that I and several others shuddered at the sight of:

@RedBeard0531
RedBeard0531 / future_traits.cpp
Last active April 26, 2018 15:53
Future then() traits
#include <tuple>
#include <optional>
#include <functional>
/////////////////////
/// PRIMARY TEMPLATES
template <typename T>
struct future_then_return_traits {
// Future::then(cb) for a callback that returns T,