CppCon 2025 Sergey Dobychin Constexpr STL Containers Limitations

CppCon, The C++ Conference 2025 Sergey Dobychin: "Constexpr STL Containers: Why C++20 Still Falls Short" youtu.be/Py4GJaCHwkA One of the most powerful features that sets C++ apart from many other languages is its ability to perform complex constant evaluation at compile time using constexpr. With each new language standard, these capabilities have expanded — and C++20 took a big step forward by allowing dynamic memory allocation in constexpr functions via operator new. This made it possible, for example, to use containers like std::vector and std::string in constexpr code. However, there’s still a key limitation: you can only use these containers in constexpr functions — not as top-level constexpr objects. That means you can't declare a constexpr std::vector or std::string at global scope and use it later in runtime without overhead for initialization. The language still doesn’t fully support top-level dynamic containers in constant expressions. In this talk, I’ll explore the current state of constant evaluation in modern C++, with a special focus on dynamic memory allocation in constexpr contexts. I’ll walk through the limitations in C++20 and show how you can go beyond them by implementing a custom allocator that enables truly compile-time std::vector, std::string, and other STL containers, which will become fully supported as constexpr in future standards, thus opening the door to more powerful and flexible compile-time programming. --- Sergey Dobychin Sergey worked in systems programming at Kaspersky Lab. Currently, he is developing desktop applications in C++.

To view or add a comment, sign in

Explore content categories