Inside STL: The Pair and the Compressed Pair in C++
The article explores the implementation of std::pair and compressed pairs in the C++ standard library. The author explains that std::pair is a simple type with fixed member names. However, compressed pairs are a more advanced version used internally by C++ library implementations. They are used to optimize the storage of empty classes in the standard library. By utilizing the empty base optimization (EBO), empty classes can be reduced to zero bytes in size. The article also mentions that the Boost library provides an implementation of compressed_pair. Additionally, the introduction of the [[no_unique_address]] attribute in C++20 offers a simpler alternative to compressed pairs. Despite this, C++ standard library implementations still rely on their internal compressed pair types. This article provides valuable insights for developers working with C++ and wanting to understand the inner workings of the standard library.