Rico's Nerd Cluster

「离开世界之前 一切都是过程」

C++ - ABI Compatibility

ABI Compatibility: Introduction and Motivating Example ABI stands for Application Binary Interface. Two versions of the same library might be API-Compatible (you can recompile your code), but they...

C++ - Pimpl Idiom

Minimal Structure 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 // header // Widget.hpp #pragma once #include <memory> class Widget { public: Widget(); ~Widget(); void doSomething...

C++ - Smart Pointers

unique_ptr, shared_ptr

Common Operations Reset: std::unique_ptr::reset() and std::shared_ptr::reset unique_ptr::reset() is basically basically delete ptr; ptr = nullptr std::shared_ptr::reset() can be a bit slower...

C++ - Streams

Stringstream, iostream, string

Streams fstream Open a file, then start appending to the file: 1 std::ofstream ofs(filename, std::ios::out | std::ios::app); How to add a custom print function? 1 2 3 4 5 inline std::...

C++ - chrono

Basic Timing: high-resolution clock 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <chrono> void foo(){ auto start = std::chrono::high_resolution_clock::now(); std::cout<<"Hello...

C++ - Manual Memory Garbage Collector

Boehm-Demers-Weiser (BDW) Collector

Boehm-Demers-Weiser Collector (BDW) Collector BDW Collector is designed as a “drop-in” replacement in C for the default memory allocation functions (malloc, free, etc.). Developers can link agains...

C++ - Iterators

Basic Usage An iterator needs to be dereferenced *it or it->first so its internal data can be accessed. Iterator Category Examples Supported Operations ...

C++ - [OOP] Members

Member Attributes and Methods, Copy, Move, Static Members

Member Attributes Let’s take a look at a tricky example 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include <iostream>...

C++ - [OOP] Polymorphism Substitute - Curiously Recurring Template Pattern (CRTP)

static polymorphism

Optimization Impact: ⭐️⭐️⚪⚪⚪ CRTP is a design pattern where the base class takes in derived class as a template parameter. This way, no virtual function is created, no runtime cost is spent on vta...

C++ - [OOP] Polymorphism - Virtual Functions and Virtual Inheritance

Virtual is virtually complicated. Dynamic Dispatch, Dreadful Diamond Derivative (DDD) Problem ...

Introduction The virutal keyword is very versatile. A really short and nasty summary is, it is to ensure the correct functions or base class is inherited / loaded during runtime. Motivating Examp...