Skip to content

Instantly share code, notes, and snippets.

View elbeno's full-sized avatar

Ben Deane elbeno

View GitHub Profile
@elbeno
elbeno / variant_fold.cpp
Last active January 19, 2017 01:26
Folding over a variant (JSON example)
#include <eggs/variant.hpp>
#include <cstddef>
#include <iomanip>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
@elbeno
elbeno / tuple_fold.cpp
Last active October 4, 2016 12:38
Folding over a tuple
#include <cstddef>
#include <cstring>
#include <cwctype>
#include <functional>
#include <iostream>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>
@elbeno
elbeno / time.cpp
Last active August 15, 2016 21:43
Time functionality snippets
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <algorithm>
#include <chrono>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <sstream>
@elbeno
elbeno / join.cpp
Last active August 21, 2016 15:52
Join algorithm
#include <algorithm>
#include <cstddef>
#include <iterator>
#include <type_traits>
#include <utility>
using namespace std;
template <typename InputIt, typename OutputIt,
typename FwdIt, typename F>
@elbeno
elbeno / random_duration.cpp
Last active November 14, 2016 14:59
Uniform distributions with chrono durations
#include <algorithm>
#include <array>
#include <chrono>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <random>
#include <type_traits>
@elbeno
elbeno / int_range.cpp
Created November 22, 2016 17:27
Integral ranges with initializer_lists and parameter packs
#include <cstddef>
#include <iostream>
#include <type_traits>
#include <utility>
using namespace std;
template <typename T, T Start, T... Ts>
const auto& make_int_range_helper(std::integer_sequence<T, Ts...>) {
static const initializer_list<int> il{ (Start+Ts)...};
@elbeno
elbeno / constexpr_u64.cpp
Created January 23, 2017 18:50
constexpr u64 mul/add, avoiding overflow warnings on VC++
constexpr uint64_t lo(uint64_t x) { return x & UINT64_C(0xffffffff); }
constexpr uint64_t hi(uint64_t x) { return x >> 32; }
constexpr uint64_t mulu64(uint64_t a, uint64_t b)
{
return lo(lo(a) * lo(b))
+ (lo(hi(lo(a) * lo(b))
+ lo(a) * hi(b)
+ hi(a) * lo(b)) << 32);
}
@elbeno
elbeno / pi_approx.cpp
Created March 5, 2017 22:46
Expressive code to (poorly) approximate pi
// 2017 Pi Day challenge:
// http://www.fluentcpp.com/2017/03/02/the-pi-day-challenge-for-expressive-code-in-c/
#include <algorithm>
#include <array>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <random>
@elbeno
elbeno / overload.h
Last active July 27, 2017 22:17
Vittorio's overload_set
#pragma once
#include <type_traits>
#include <utility>
#if __cplusplus >= 201703L
// C++17 has variadic using
template <typename... Fs>
@elbeno
elbeno / exchange_for_move_operations.cpp
Created September 17, 2017 17:33
Using std::exchange to implement move construction/assignment
#include <algorithm>
#include <cassert>
#include <iostream>
#include <memory>
#include <utility>
using namespace std;
struct Foo
{