Skip to content

Instantly share code, notes, and snippets.

View nthery's full-sized avatar

Nicolas Thery nthery

  • Grenoble - France
View GitHub Profile
@nthery
nthery / bean_deane_global_api_injection.cpp
Created September 24, 2023 17:58
Ben Deane's global dependency injection pattern
// Ben Deane's global dependency injection pattern.
// https://www.youtube.com/watch?v=BYpcAEfG3mo
#include <utility>
//
// Header providing service and its default implementation.
//
struct default_logger {
@nthery
nthery / tree_eval_variant_visit.cpp
Last active September 22, 2023 04:48
Toy expression tree evaluator showing std::visit() and overloaded {} sugar
// Toy expression tree evaluator showing std::visit() and overloaded {} sugar.
//
// https://gcc.godbolt.org/z/r7PffK18e
//
// Diff view for older version showing generated assembly is same with or without overloaded:
// https://godbolt.org/z/6efWvj5o3
#include <variant>
#include <memory>
#include <string>
// Of builders and temporary materialisation
void hotSpot() noexcept;
// ORIGINAL CODE
// RAII wrapper to start and stop a timer.
struct Timer {
// Start timer
explicit Timer(bool isFoo = true, bool isBar = false, bool isBaz = false);
// Example showing when temporary materialisation and copy elision occurs.
struct S {
S();
~S();
S(const S&);
S& operator=(const S&);
S(S&&) noexcept;
S& operator=(S&&) noexcept;
@nthery
nthery / mvs_ast.cpp
Last active January 8, 2023 11:07
Tiny AST and expression evaluator inspired by Dave Abrahams' Mutable Value Semantic talk.
// Tiny AST and expression evaluator inspired by Dave Abrahams' Mutable Value Semantic talk.
#include <string>
#include <variant>
#include <vector>
#include <iostream>
#include <map>
// Strong identifier.
struct NodeId {
@nthery
nthery / map_filter_ranges_vs_stl.cpp
Last active December 5, 2022 13:18
Benchmark map-filter implemented with STL and ranges
#include <vector>
#include <functional>
#include <range/v3/all.hpp>
namespace rv= ranges::view;
inline bool is_odd(int n) {
return n & 1;
}
@nthery
nthery / variant_expression_tree.cpp
Last active November 6, 2022 15:03
Expression tree represented by a std::variant
// std::variant expression tree
#include <functional>
#include <iostream>
#include <memory>
#include <sstream>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <unordered_map>
@nthery
nthery / getters_with_ref_qualifiers.cpp
Created October 5, 2022 08:25
Show how to use ref qualifiers to prevent dangling references to member variable
// Show how to use ref qualifiers to prevent dangling references to member variables.
struct S {
const int& f() const& { return n_;}
const int& h() & { return n_; }
const int& g() && { return n_; }
const int& l() const & { return n_; }
// Range demo
#include <iostream>
#include <algorithm>
#include <iterator>
#include <range/v3/all.hpp>
void first_class_range() {
// Classic STL: A range is just a pair of iterators
#include <utility>
struct Bar {
int *payload_;
Bar();
~Bar();
Bar(const Bar&);
Bar(Bar&&) noexcept;
Bar& operator=(const Bar&);