Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does constexpr new allocate memory?

How can we dynamically allocate at compile time? Does the constexpr operator new just allocate memory on the stack?

like image 366
Tachi Avatar asked Dec 02 '22 09:12

Tachi


1 Answers

There is no constexpr new operator.

Since C++20, you can use new operator in constexpr expressions in the condition that you only use a replaceable global allocation function (it means that you don't use a placement new or user-defined allocation function) and that you deallocate the data in the same expression.

So, in your final program, this does not allocate memory, since you end up just with the final result of your constexpr expression.

like image 196
Nicolas Dusart Avatar answered Dec 30 '22 09:12

Nicolas Dusart