Skip to content

Instantly share code, notes, and snippets.

View ecatmur's full-sized avatar
👻

Ed Catmur ecatmur

👻
  • Maven Securities
  • Chicago
View GitHub Profile
@ecatmur
ecatmur / gist:cf7bc1e0e79ee6fe408e
Last active August 29, 2015 14:01
odr-use fixes for array subscripting/indirection

To 3.2p2, add:

  • if e is a subscript expression (5.2.1), where one operand is of type "array of N T" or "reference to array of N T" and the other operand is a constant expression of integral or enumeration type with value n, where 0 <= n < N, the set contains the potential results of the operand of array type.
  • if e is an indirection expression (5.3.1), the set contains the potential results of the operand.

Modify 3.2p3 thus:

A variable x whose name appears as a potentially-evaluated expression ex is odr-used by ex unless applying the lvalue-to-rvalue conversion (4.1) to x or, if x is of array type, an element of x yields a constant expression (5.19) that does not invoke any nontrivial functions and, if x is an object, ex is an element of the set of potential results of an expression e, where either the lvalue-to-rvalue conversion (4.1) is applied to e, or e is a discarded-value expression (Clause 5).

@ecatmur
ecatmur / cic.cpp
Created August 8, 2014 10:15
Contextual implicit conversion
# https://groups.google.com/a/isocpp.org/forum/#!topic/std-discussion/rvbQiAx38Tg%5B1-25-false%5D
# http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3323.pdf
struct C1 { enum class A{}; operator A(); explicit operator unsigned(); } c1;
struct C2 { operator char*(); explicit operator bool(); } c2;
struct C3 { operator int() = delete; operator unsigned(); } c3;
struct C4 { explicit operator int() = delete; operator unsigned(); } c4;
struct C5 { operator int(); explicit operator unsigned(); } c5;
void f() {
switch (c1) {}
@ecatmur
ecatmur / max_element.c++
Created September 3, 2014 09:48
max_element benchmarking
#include <nonius/main.h++>
#include <vector>
#include <algorithm>
namespace my {
int* max_element1(int* first, int* last)
{
int* result = first;
#!/usr/bin/python3
import string
import subprocess
import sys
import random
from argparse import ArgumentParser, SUPPRESS
from gi.repository import GLib, Gio
@ecatmur
ecatmur / generator.cpp
Created September 6, 2012 22:49
Classless reinvocable functions using lambdas
#include <iostream>
#include <vector>
#include <functional>
#include <memory>
#include <type_traits>
#include <tuple>
#include <algorithm>
#include <stdexcept>
template<typename R, typename... Args> class generator_base {
#include <iostream>
template<char>struct k;template<>struct k<2>{static
constexpr char t[]=".....\n";};template<>struct k<4>{static
constexpr char t[]=" ";};template<>struct k<8>{static
constexpr char t[]=".abcde";};template<>struct k<9>{static
constexpr char t[]=".jklmnopqr";};template<>struct k<0xc>{static
constexpr char t[]="{ABCDEFGH";};template<>struct k<0xe>{static
constexpr char t[]="\\.STUVW";};constexpr int h(char c){return
c=='a'?0xa:c=='b'?0xb:c=='c'?0xc:c=='d'?0xd:c=='e'?0xe:c=='f'?0xf:c-'0';}template<char
c,char d>struct a{constexpr static
@ecatmur
ecatmur / gist:4683098
Last active December 12, 2015 00:19 — forked from lcapaldo/gist:2065956
State monad, with nary lift. Type construction is collapsed to allow inference.
#include <functional>
#include <utility>
#include <iostream>
namespace hask {
struct Unit {};
template<typename S, typename V> struct State {
std::function<std::pair<S, V>(S)> runState;
using state_type = S;
using value_type = V;
#include <iostream>
template<typename T, typename U> class wrapped_value {
T t;
U u;
wrapped_value(T t, U u): t(std::move(t)), u(std::move(u)) {}
public:
explicit operator bool() const { return u; }
friend T &unwrap(wrapped_value &w) { return w.t; }
template<typename T2, typename V> friend class wrapped_value;
#include <vector>
#include <iostream>
#include <iomanip>
struct Row {
int off;
std::vector<double> buf{};
double b = -4.;
};
// 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)...); }; }); };