Open utterances-bot opened 3 years ago
Nice. Pity you can't use std::make_unique() within a constexpr/consteval function. Or even std::unique_ptr.
Shouldn't std::make_unique() now be constexpr - so that it can be used in a constexpr function? C++23?
interesting. Some time back I remember trying to do some constexpr coding with std containers like vector and string and it was rejected. looks like things changed. thanks.
@2kaud:
I was asking myself the same question and found the following proposal:
P2273 R0 Making std::unique_ptr constexpr
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2273r0.pdf
Thanks @HelmutLinz ! I have to update the article and mention unique_ptr in the limitation section... and link to that paper. It tells that without unique_ptr programmers are left with some old-style resource management...
https://godbolt.org/z/b5ah9EscM
It does not warn me sbout the memory leak?
@DBJDBJ this code is executed at runtime - not in the constexpr context - so there's no leak detection now.
@fenbf, Perhaps clarification it in the article, would not be out of order :)
And clangs inability to do the same ( because of std::iota
and the rest ). Perhaps too much detail, good article anyway.
It might be interesting to make the 2021 version of : https://github.com/fenbf/review/blob/master/stl/beautiful_std_alg.cpp
@DBJDBJ ok, I'll add additional clarification, as it might be confusing when your code is run at compile-time vs runtime. And yep - a lot of my old content could be updated to 2021 :)
a lot of my old content could be updated to 2021
Not exactly what I meant. That code is ok for C++ 2014 :) What I precisely meant is: (try to) make constexpr
C++ 2021 version and compare it to the 2014 version. In the spirit of this good article.
I am leaving now much to everyone's relief 😄
If constexpr is replaced with consteval, then if the function cannot be performed at compile time then the compiler produces an error - rather than deferring execution until run-time as with constexpr.
There is also std::is_constant_evaluated() which detects whether the function in which this is evaluated occurred within a constant-evaluated context. See https://en.cppreference.com/w/cpp/types/is_constant_evaluated
[How do you edit your own post? There's a duplicate word in my post above]
@2kaud to edit you can visit github and then edit the message: https://github.com/fenbf/cppstories-discussions/issues/14#issuecomment-806510020 You can also click on "N minutes ago" in the header of your comment and that will move you to the github page
In C++20 you can even use std::vector and std::string in constexpr context!
Not implemented in VS2019 until 16.10. Ahh!
In C++20 you can even use std::vector and std::string in constexpr context!
Not implemented in VS2019 until 16.10. Ahh!
yep! let's wait a bit on that :)
Hello! Thanks for the blog. Just checking the new comments section. :D
Wonderful! I can't find any blog on this topic as excellent as yours in chinese community. Can I translate your article in to chinese without commercial use? I will put your name and link at the beginning of translation. Even if it is rejected, thank you very much still.
hello @itungsten yes, sure! please notify us when the article is ready and I'll link it from C++ Stories.
@DBJDBJ wrote:
And clangs inability to do the same ( because of
std::iota
and the rest ).
Seems working in 13: https://godbolt.org/z/oqrTGbWW4
Seems working in 13: https://godbolt.org/z/oqrTGbWW4
Thanks. Works in clang 11 too
And BTW, that smartSum()
turns the modern std::
into the laughing stock. At least for me.
constexpr Dynamic Memory Allocation, C++20 - C++ Stories
constexpr has become a major feature for compile-time programming in C++. Introduced in a simple form in C++11 evolved into almost another “sub-language”, an alternative to regular template code. In C++20 you can even use std::vector and std::string in constexpr context! In this article, I’d like to discuss constexpr memory allocations, a building block for std::vector.
https://www.cppstories.com/2021/constexpr-new-cpp20/