Skip to content

Instantly share code, notes, and snippets.

View martinmoene's full-sized avatar

Martin Moene martinmoene

View GitHub Profile
@martinmoene
martinmoene / clamp-range-v3.cpp
Created August 23, 2015 19:57
C++ example of proposed clamp() used with range-v3 library.
// clamp-range-v3
#include <iostream>
#include <clamp.hpp>
#include <range/v3/core.hpp>
#include <range/v3/view/iota.hpp>
#include <range/v3/view/remove_if.hpp>
#include <range/v3/view/transform.hpp>
@martinmoene
martinmoene / value-semantics-sean-parent.cpp
Created August 18, 2015 15:07
Code from talk: Inheritance Is The Base Class of Evil by Sean Parent at Going Native 2013
// Sean Parent. Inheritance Is The Base Class of Evil. Going Native 2013
// Video: https://www.youtube.com/watch?v=bIhUE5uUFOA
// Code : https://github.com/sean-parent/sean-parent.github.io/wiki/Papers-and-Presentations
/*
Copyright 2013 Adobe Systems Incorporated
Distributed under the MIT License (see license at
http://stlab.adobe.com/licenses.html)
This file is intended as example code and is not production quality.
@martinmoene
martinmoene / catch-main.cpp
Last active September 6, 2017 01:43
CATCH - Small complete multi-file example.
// This tells Catch to provide a main() - only do this in one cpp file:
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
@martinmoene
martinmoene / main.cpp
Last active August 29, 2015 14:22
FizzBuzz GUI
#include <iostream>
#include <string>
#define sometimes if
bool is( std::string text )
{
return ! text.empty();
}
std::string number( int x )
@martinmoene
martinmoene / loop_vs_algorithm.cpp
Last active August 29, 2015 14:21
Replacing a loop (compute_loop()) with an std::algorithm.
// replacing a loop (compute_loop()) with an std::algorithm.
#include <algorithm>
#include <functional>
#include <vector>
using Coll = std::vector<int>;
int compute_loop( Coll a, Coll b )
{
@martinmoene
martinmoene / lest-module-1.cpp
Created December 26, 2014 11:16
Spread lest tests over multiple files
// C++11 - use multiple source files.
#include "lest.hpp"
lest::tests & specifications()
{
static lest::tests tests;
return tests;
}
@martinmoene
martinmoene / Understand-C++
Last active October 16, 2022 20:21
Myth 1: “To understand C++, you must first learn C” -- performance aspect
// https://isocpp.org/blog/2014/12/myths-1
//
// Bjarne Stroustrup.
//
// 2. Myth 1: “To understand C++, you must first learn C”
//
// No. Learning basic programming using C++ is far easier than with C.
//
// C is almost a subset of C++, but it is not the best subset to learn first
// because C lacks the notational support, the type safety, and the easier-to-use
// lest light-weight sections
// [1] Catch: Test cases and sections,
// https://github.com/philsquared/Catch/blob/develop/docs/tutorial.md#test-cases-and-sections
// [2] lest – lest errors escape testing,
// https://github.com/martinmoene/lest#lest--lest-errors-escape-testing---10
#include <cassert>
#include <iostream>
@martinmoene
martinmoene / gist:9485071
Last active August 29, 2015 13:57
with scoped
// [std-proposals] Using the underscore for unused identifiers
// https://groups.google.com/a/isocpp.org/d/msg/std-proposals/a4CRu2KONZ8/N0aPF76B990J
#include <iostream>
// _, or ANON
#define with( expr ) if ( auto&& _ = (expr) )
struct wither
{
@martinmoene
martinmoene / gist:9410391
Created March 7, 2014 12:13
std::between() an idea by Niels Dekker
// std::between() an idea by Niels Dekker
#include <functional>
namespace std {
template<class T>
bool between(T const& val, T const& lo, T const& hi)
{
return between( val, lo, hi, std::less<T>() );