This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <functional> | |
#include <range/v3/all.hpp> | |
namespace rv= ranges::view; | |
inline bool is_odd(int n) { | |
return n & 1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// std::variant expression tree | |
#include <functional> | |
#include <iostream> | |
#include <memory> | |
#include <sstream> | |
#include <stdexcept> | |
#include <string> | |
#include <type_traits> | |
#include <unordered_map> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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_; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <utility> | |
struct Bar { | |
int *payload_; | |
Bar(); | |
~Bar(); | |
Bar(const Bar&); | |
Bar(Bar&&) noexcept; | |
Bar& operator=(const Bar&); |
NewerOlder