Skip to content

Instantly share code, notes, and snippets.

View elbeno's full-sized avatar

Ben Deane elbeno

View GitHub Profile
@elbeno
elbeno / emacs-cppcon-2018.org
Created September 30, 2018 02:33
Mini emacs presentation for CppCon 2018
# -*- mode: org -*-
#+OPTIONS: reveal_center:t reveal_progress:t reveal_history:t reveal_control:t
#+OPTIONS: reveal_mathjax:t reveal_rolling_links:nil reveal_keyboard:t reveal_overview:t num:nil
#+OPTIONS: reveal_width:1600 reveal_height:900
#+OPTIONS: toc:nil ^:nil <:nil timestamp:nil email:t reveal_slide_number:"c/t"
#+REVEAL_MARGIN: 0.1
#+REVEAL_MIN_SCALE: 0.5
#+REVEAL_MAX_SCALE: 2.5
#+REVEAL_TRANS: none
#+REVEAL_THEME: blood
@elbeno
elbeno / ykloc.awk
Last active June 19, 2018 09:20
ykloc: measure year-kilo-lines-of-code per author
{
# fields between ( and )
start = index($0, "(") + 1
len = index($0, ")") - start
$0 = substr($0, start, len)
# field n-2 is the timestamp
line_age = systime() - $(NF - 2)
# fields before timestamp are author
author = $1
@elbeno
elbeno / req_fields.cpp
Last active April 5, 2018 17:02
Setting required fields & optional fields in a type safe way
#include <iostream>
#include <string>
#include <vector>
template <uint32_t N>
struct request;
template <>
struct request<0>
{
@elbeno
elbeno / scmchallenge_cppcon2017.cpp
Created September 29, 2017 14:21
Herb Sutter's SCM Challenge at CppCon 2017 - my winning code
#include <algorithm>
#include <iostream>
#include <type_traits>
class CIString {
std::string s;
public:
CIString(std::string _s): s(_s) {};
@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
{
@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 / 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 / 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 / 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 / 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>