Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ constexpr functions with dynamic memory allocations

Tags:

c++

c++20

As an illustrative example, given a constexpr string literal, I want to transform that into another constexpr data structure. Since this transformation is purely side-effect free, I'd wish for a way to do that at compile time (without a preprocessor but with standard C++ language features).

Now, I am certain that at one point I read about a potential future feature addition to C++2a (or later) that would allow dynamic memory allocation within a constexpr function, that would definitely be the solution to my problem. - but I cannot find the article, that was telling me that, anymore.

Is there a way to perform dynamic memory allocations within a constexpr context in C++ already, or does anyone know which paper is proposing that?

like image 918
christianparpart Avatar asked Jun 23 '18 17:06

christianparpart


1 Answers

Such feature is scheduled for C++20. However, the allocated memory must not leak into the runtime. This might be problematic if your constexpr data structure needs to hold the allocated memory. You can find more information about the feature and why non-transient allocations are problematic in P0784.

like image 130
JojOatXGME Avatar answered Nov 14 '22 23:11

JojOatXGME