When would someone use std::enable_shared_from_this

std::enable_shared_from_this allows a class to have a valid shared_ptr of this. Simply adding a member function that returns shared_ptr<T>(this) is susceptible to double-free. But when would you use such a feature? It's a relevant question because, next time when you…

Build `folly::coro` with GCC

You have heard about Coroutine in C++, and you want to use it. There're two coroutine implementations that are considered most mature - cppcoro and folly::coro. They are written by the same guy - Lewis Baker. He's brilliant, and you shoud watch his cppcon talk on structured concurrency if…

SeqLock

Sequential lock is a common technique used to protect data that's frequently read and rarely updated. Writers are required to take exclusive lock to mutate the data. Reads are effectively lock free and optimistic. The data protected by a SeqLock usually needs to be trivially_copy_constructible. // reader std::atomoc&…

`folly::Indestructible`

folly::Indestructible<T> is a class template that makes a static variable well, indestructible. Notice that it's meant for static variables in the Meyers Singleton pattern. If it's for heap allocated memory, it would just be called memory leak instead of "indestructible". It boils down to making a…

C++ Map Lookup Memoization

Memoization is an old technique. It's basically caching outputs for giving inputs (usually in a map). But a map lookup itself is not free. How can we memoize map lookups? I learned from my coworker this nifty trick recently. Let's say we have a map (e.g. std::unordered_map&…

C++ Type Erasure

You have heard about Type Erasure of C++. You probably know std::function is a classic usage of the pattern. But what is Type Erasure in C++? It's effectively trying to achieve Duck Typing, which is common in languages like Python. # Notice that there's no relationship between Bar1 or Bar2…

atomic_thread_fence

Just like you can have a std::atomic synchronizes two threads with each other with release-acquire semantic, you can also have Fence-Fence, Atomic-Fence, Fence-Atomic synchronizations. C++ reference has very detailed documentation about when there exists a valid synchronizes-with relationship, https://en.cppreference.com/w/cpp/atomic/atomic_thread_fence. Rust's…

How C++ `typeid` operator works

C++ language include an operator called typeid (https://en.cppreference.com/w/cpp/language/typeid). Queries information of a type. Used where the dynamic type of a polymorphic object must be known and for static type identification. It gives you information about the type of an object, as long as…

GNU Visibility Attribute

When going through the folly code base, sometimes, you see definitions like class FOLLY_EXPORT OptionalEmptyException : public std::runtime_error { ... FOLLY_EXPORT is defined as #define FOLLY_EXPORT __attribute__((__visibility__("default"))) It sets the visibility attribute of symbol OptionalEmptyException to "default". What does it do exactly,…