Implementing static_vector: How Hard Could it Be? - David Stone - CppCon 2021

Implementing C++'s static_vector: Lessons learned on making objects relocatable and efficient storage.

Key takeaways
  • The speaker explains that implementing static_vector is challenging due to the need for trivially relocatable objects, which means that objects can be moved and destroyed without executing any code.
  • The speaker suggests that static_vector can be made more efficient by using “placement new” which is a way to construct an object at a specific location in memory, without calling the object’s constructor.
  • The speaker notes that static_vector does not allocate memory, but instead uses “stack-based” storage, which can be faster and more efficient.
  • The speaker explains that there are two types of move constructors: trivial and non-trivial. Trivial move constructors simply copy the contents of the source object to the destination, while non-trivial move constructors perform complex operations.
  • The speaker says that the key to implementing static_vector is to make objects trivially relocatable.
  • The speaker explains that uninitialized copy should not overlap with the destination, and that the destination should be treated as a range of uninitialized values.
  • The speaker notes that static_vector does not support most types, but it can be used with some types such as integers.
  • The speaker suggests that the C++ standard library should support static_vector, and that it could be useful for certain applications.
  • The speaker explains that static_vector is used in some productivity applications, where performance is critical.
  • The speaker notes that static_vector’s memory layout is compact, but that its size can vary depending on the type of object being stored.
  • The speaker explains that the move constructor for static_vector is easy to implement, and that it simply copies the contents of the source object to the destination.
  • The speaker suggests that the C++ standard library should support static_vector with a stack-based allocator.
  • The speaker explains that some compilers, such as GCC, already implement static_vector with a stack-based allocator.
  • The speaker notes that static_vector does not support all types of objects, but that it can be used with some types such as integers.
  • The speaker suggests that the C++ standard library should support static_vector, and that it could be useful for certain applications.
  • The speaker explains that static_vector is used in some productivity applications, where performance is critical.