From Jason Turner
Watch on YouTube

Summary

This episode explains the Small String Optimization (SSO) technique used in standard library classes like std::string, std::any, and std::function to avoid heap allocations for small objects. Jason demonstrates how to implement a simplified string class that uses a union to store either a small buffer for short strings or allocated memory for longer strings. He shows how constexpr can help catch bugs in this tricky implementation, which requires careful bookkeeping to track which union member is active.

Resources

Tags