Skip to content

Instantly share code, notes, and snippets.

@flomnes
flomnes / builder.cc
Last active March 25, 2025 10:04
Enforce order of builder
class SystemBuilder
{
private:
class SystemBuilderConnections
{
public:
SystemBuilderConnections(SystemBuilder& parent):
parent(parent)
{
}
@flomnes
flomnes / move.cpp
Created March 10, 2025 09:50
Order execution
#include <iostream>
#include <string>
#include <string_view>
#include <utility>
template<class T>
T&& log(T&& in, std::string_view msg)
{
std::cout << msg << std::endl;
return std::move(in);
@flomnes
flomnes / benchmark.cpp
Last active February 18, 2025 10:18
benchmark hash_map vs string map
#include <chrono>
#include <cstdlib> //rand
#include <iostream>
#include <map>
#include <string>
#include <unordered_map>
#include <boost/container_hash/hash.hpp>
constexpr int INV_REQ = 2;
@flomnes
flomnes / reserve_vec.cpp
Created January 24, 2025 12:37
Show how std::vector<>::reserve can avoid copies and reallocations
#include <iostream>
class C
{
public:
C()
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
#include <algorithm>
#include <vector>
static bool operator<=(const std::vector<double>& v, const double c)
{
return std::ranges::all_of(v, [&c](const double& e) { return e <= c; });
}
static bool operator>=(const std::vector<double>& v, const double c)
{
@flomnes
flomnes / hashable.cpp
Created September 24, 2024 17:26
hashable factorization
#include <string>
#include <utility>
class Hashable
{
public:
Hashable(const std::string& s1, const std::string& s2):
s1(s1),
s2(s2)
{
@flomnes
flomnes / simplification.cpp
Last active September 4, 2024 16:56
AST Simplification
#include <optional>
#include <string>
#include <utility>
class Node
{
public:
virtual ~Node() = default;
};
@flomnes
flomnes / default operator.cpp
Created August 26, 2024 13:49
Default operator error
class EmptyClass
{
virtual ~EmptyClass() = default;
};
class DerivedFromEmpty: public EmptyClass
{
bool operator==(const DerivedFromEmpty&) const = default;
};
@flomnes
flomnes / exception_visitor.cpp
Created August 23, 2024 13:26
Exception throw in visitors
#include <stdexcept>
class IName
{
public:
virtual const std::string& name() const = 0;
};
class EqualNode: public IName
@flomnes
flomnes / enumclass.cpp
Created August 20, 2024 13:33
Operator overloading with enum class
#include <cassert>
enum class C
{
LINEAR,
NON_LINEAR,
CONSTANT
};
C operator+(C x, C y)