People often say constexpr all the things. Andreas Fertig shows where we can use dynamic memory at compile time.
C++20 Dynamic Allocations at Compile-time
By Andreas Fertig
From the article:
You may already have heard and seen that C++20 brings the ability to allocate dynamic memory at compile-time. This leads to
std::vector
andstd::string
being fullyconstexpr
in C++20. In this article, I like to give you a solid idea of where you can use that.How does dynamic allocation at compile-time work?
First, let’s ensure that we all understand how dynamic allocations at compile-time work. In the early draft of the paper ‘Standard containers and constexpr’ [P0784R1], proposed so-called non-transient allocations. They would have allowed us to allocate memory at compile-time and keep it to run-time. The previously allocated memory would then be promoted to static storage. However, various concerns did lead to allowing only transient allocations. That means what happens at compile-time stays at compile-time. Or in other words, the dynamic memory we allocate at compile-time must be deallocated at compile-time. This restriction makes a lot of the appealing use-cases impossible. I personally think that there are many examples out there that are of only little to no benefit.
Add a Comment
Comments are closed.