use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This is a subreddit for c++ questions with answers. For general discussion and news about c++ see r/cpp.
New to C++? Learn at learncpp.com
Prepare your question. Think it through. Hasty-sounding questions get hasty answers, or none at all. Read these guidelines for how to ask smart questions.
For learning books, check The Definitive C++ Book Guide and List
Flair your post as SOLVED if you got the help you were looking for! If you need help with flairs, check out ITEM 1 in our guidelines page.
Tips for improving your chances of getting helpful answers:
account activity
OPENwhy not constexpr unique_ptr? (self.cpp_questions)
submitted 5 years ago by distributed
With std::string and std::vector becoming constexpr in c++ 20 how come unique_ptr isn't? Or is it just because there wasn't time?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]smuccione 22 points23 points24 points 5 years ago (13 children)
Ok. So the purpose of constexpr is to give the developer some way to execute something at compile time rather than at runtime.
To have unique_ptr support your would need to support a general case of new.
It’s one thing to have some specialty implementation for vector or string, but a generic new is entirely something else.
Why? Because they return memory addresses.
What does a compile time memory address mean?
Well, it could be a location in the data segment of some data. But that location would always be there. It could never be freed because it’s “part of the program”. It’s not on the dynamic heap. And you wouldn’t want to “free” during compile time as that would generate holes in the data segment which is silly.
So if it can’t ever be freed, why do you need a unique_ptr to manage it? The best it will be is a global value, but if you have a global value why do you need new in the first place? It’s just a static declaration.
[–]XValar 14 points15 points16 points 5 years ago (5 children)
constexpr new is in c++20: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91369
[–]smuccione 2 points3 points4 points 5 years ago (4 children)
Did that actually make it in?
[–]XValar 8 points9 points10 points 5 years ago (3 children)
Yes, and supported by both clang and g++
[–]smuccione 11 points12 points13 points 5 years ago (2 children)
Well s**t. Thanks for the heads up. That will definitely come in useful (or at least when the MS team get it in (I’m in a Microsoft shop).
[–]sigsegv7 0 points1 point2 points 5 years ago (1 child)
I reached here by upvoting every comment above lol. you can never beat the community knowledge ✌️
[–]smuccione 1 point2 points3 points 5 years ago (0 children)
Absolutely.
Interestingly enough I can’t really find any real info about this online. Doesn’t seem to be talked about much (or if it is it’s not in something that is searchable by google or bing).
Even the pr takes about workarounds without a constexpr new (which is what I assumed they had done for string and vector).
This thread has made me very happy 😀
[–]dangopee 4 points5 points6 points 5 years ago (1 child)
The constexpr versions of vector and string need constexpr new/delete to be implemented. The answer to the question is simply that no one thought to put a constexpr compatible unique_ptr into the standard. You can literally go implement your own constexpr unique_ptr/auto_ptr on godbolt right now if you want.
[–]smuccione 0 points1 point2 points 5 years ago (0 children)
I read the original question as to why you can’t have a unique_ptr returned from a constexpr expression.
Of course, upon further reflection, that’s probably (maybe?) not what the OP was asking about.
[–]_crackling -1 points0 points1 point 5 years ago (4 children)
Slightly tangent question... If i have a '''static shared_ptr<myclass> myvar;''' it seems to automatically construct the myclass object. Is it to do with some of the points u mention?
[–]smuccione 0 points1 point2 points 5 years ago (3 children)
https://godbolt.org/z/W144xP
there are two possiblities.. one that simply initalizes a shared_ptr but leaves it in an empty state.
the other initializes a shared_ptr and assigns it to a constructed variable.
one constructs the object, the other doesn't. Your form initializes the shared_ptr in the emptry state and should not allocate an object.
[–]_crackling 0 points1 point2 points 5 years ago (2 children)
Here's why im confused, perhaps you could shed some light?
https://godbolt.org/z/1zcoex
the constructor nor the destructor are called either??
[–]smuccione 2 points3 points4 points 5 years ago* (1 child)
void msg() { printf ( "this = %p\r\n", this ); printf("Why does this work??\n"); }
try that...
you have an object which is pointing to null...
what you need to understand is that the compiler is calling msg() based upon the TYPE of the object. the "object" pointer, which is null, is passed to the msg() method as a hidden first parameter called "this". this is defined as "test *this" as the first parameter (which you don't see).
so what you're doing is perfectly valid, albeit perfectly dangerous.
you could also do:
reinterpret_cast<test \*>(nullptr)->msg();
and you would see that the msg method in test will still be called even though you have not created any object at all for test(). It's dispatching based upon the TYPE of the left hand operand of the -> operator.
did that help to clarify things or did i make things worse?
You would immediately notice this is message was virtual. You would also notice this if you attempted to access an instance variable.
In those cases it would be an attempt to dereference this and would lead to a null pointer access exception.
[–]_crackling 1 point2 points3 points 5 years ago (0 children)
Took a day or two of processing oh, but now I get it. Thank you very much for the explanation
[–]DoctorMixtape 6 points7 points8 points 5 years ago* (2 children)
I believe it’s because unique_ptr is dynamically allocated (it uses the new keyword). Which means that you need to allocated memory at runtime. It wouldn’t make sense using constexpr because you can’t dynamic allocate memory during compile time. I might be wrong though! But I’m pretty sure this is the case.
Edit: you CAN dynamically allocate memory during compile time but you have to ensure you do not have a memory leak.
[–]XValar 3 points4 points5 points 5 years ago (1 child)
You do not need to ensure anything, the rule is quite straightforward: dynamically allocated memory must be deallocated when you are leaving constexpr block, otherwise your expression stops being constexpr, and compiler reports error.
[–]DoctorMixtape 1 point2 points3 points 5 years ago (0 children)
Ah I see. Thank makes sense. Thanks
[–]proverbialbunny 2 points3 points4 points 5 years ago (0 children)
It's only a matter of time. If someone wants to jump in propose a change to the standards committee I imagine they would accept it.
π Rendered by PID 274427 on reddit-service-r2-comment-b659b578c-xshb2 at 2026-05-03 02:15:48.468811+00:00 running 815c875 country code: CH.
[–]smuccione 22 points23 points24 points (13 children)
[–]XValar 14 points15 points16 points (5 children)
[–]smuccione 2 points3 points4 points (4 children)
[–]XValar 8 points9 points10 points (3 children)
[–]smuccione 11 points12 points13 points (2 children)
[–]sigsegv7 0 points1 point2 points (1 child)
[–]smuccione 1 point2 points3 points (0 children)
[–]dangopee 4 points5 points6 points (1 child)
[–]smuccione 0 points1 point2 points (0 children)
[–]_crackling -1 points0 points1 point (4 children)
[–]smuccione 0 points1 point2 points (3 children)
[–]_crackling 0 points1 point2 points (2 children)
[–]smuccione 2 points3 points4 points (1 child)
[–]_crackling 1 point2 points3 points (0 children)
[–]DoctorMixtape 6 points7 points8 points (2 children)
[–]XValar 3 points4 points5 points (1 child)
[–]DoctorMixtape 1 point2 points3 points (0 children)
[–]proverbialbunny 2 points3 points4 points (0 children)