public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/103668] New: constexpr std::vector not working as expected
@ 2021-12-12 13:49 andrei.popa105 at yahoo dot com
2021-12-12 16:40 ` [Bug c++/103668] " redi at gcc dot gnu.org
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: andrei.popa105 at yahoo dot com @ 2021-12-12 13:49 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103668
Bug ID: 103668
Summary: constexpr std::vector not working as expected
Product: gcc
Version: 12.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: andrei.popa105 at yahoo dot com
Target Milestone: ---
Hi,
Currently gcc supports constexpr std::vector and std::string. Suppose that you
want to write code like this:
#include <vector>
constexpr auto get_vector() {
std::vector<int> vec{ 1, 2, 3, 4, 5, 6 };
return vec;
}
constexpr auto get_vector_size() {
constexpr auto vec = get_vector();
return vec.size();
}
int main() {
constexpr auto vec_size = get_vector_size();
return 0;
}
Unfortunately, this code does not compile and the error is the following:
error: ‘get_vector()()’ is not a constant expression because it refers to a
result of ‘operator new’
But std::vector is not used in runtime world, the function get_vector is called
in a constexpr context and then returns the size of the vector. Am I missing
something or it is a bug?
I compiled this code with g++ file.cpp -std=c++23
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/103668] constexpr std::vector not working as expected
2021-12-12 13:49 [Bug c++/103668] New: constexpr std::vector not working as expected andrei.popa105 at yahoo dot com
@ 2021-12-12 16:40 ` redi at gcc dot gnu.org
2021-12-12 16:58 ` andrei.popa105 at yahoo dot com
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2021-12-12 16:40 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103668
--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I don't think this is valid C++.
A vector created in a constexpr function must be destroyed there as well, it
cannot escape from the function.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/103668] constexpr std::vector not working as expected
2021-12-12 13:49 [Bug c++/103668] New: constexpr std::vector not working as expected andrei.popa105 at yahoo dot com
2021-12-12 16:40 ` [Bug c++/103668] " redi at gcc dot gnu.org
@ 2021-12-12 16:58 ` andrei.popa105 at yahoo dot com
2021-12-12 17:54 ` jakub at gcc dot gnu.org
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: andrei.popa105 at yahoo dot com @ 2021-12-12 16:58 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103668
--- Comment #2 from Andrei-Edward Popa <andrei.popa105 at yahoo dot com> ---
This code is valid in MSVC compiler, that's why I'm wondering about it.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/103668] constexpr std::vector not working as expected
2021-12-12 13:49 [Bug c++/103668] New: constexpr std::vector not working as expected andrei.popa105 at yahoo dot com
2021-12-12 16:40 ` [Bug c++/103668] " redi at gcc dot gnu.org
2021-12-12 16:58 ` andrei.popa105 at yahoo dot com
@ 2021-12-12 17:54 ` jakub at gcc dot gnu.org
2021-12-12 18:01 ` andrei.popa105 at yahoo dot com
2021-12-12 20:29 ` redi at gcc dot gnu.org
4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-12-12 17:54 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103668
Jakub Jelinek <jakub at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |jakub at gcc dot gnu.org
--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #1)
> I don't think this is valid C++.
>
> A vector created in a constexpr function must be destroyed there as well, it
> cannot escape from the function.
Not a C++ lawyer, but I think returning it from constexpr function is just
fine,
what is wrong is if it isn't destructed in the same constant evaluation as the
construction. So the bug in the testcase IMHO is the constexpr keyword before
auto vec = get_vector();, that means one constant evaluation is the computation
of the initializer of vec and that doesn't destruct what it constructed.
Now, if you remove that bogus constexpr keyword or replace it with const
keyword,
it works just fine, vec is constructed, vec.size() is computed and then vec is
destructed.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/103668] constexpr std::vector not working as expected
2021-12-12 13:49 [Bug c++/103668] New: constexpr std::vector not working as expected andrei.popa105 at yahoo dot com
` (2 preceding siblings ...)
2021-12-12 17:54 ` jakub at gcc dot gnu.org
@ 2021-12-12 18:01 ` andrei.popa105 at yahoo dot com
2021-12-12 20:29 ` redi at gcc dot gnu.org
4 siblings, 0 replies; 6+ messages in thread
From: andrei.popa105 at yahoo dot com @ 2021-12-12 18:01 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103668
--- Comment #4 from Andrei-Edward Popa <andrei.popa105 at yahoo dot com> ---
Yes, I really missed this constexpr instead of const, this is clear for me.
Thank you!
I think this thread can be closed now.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/103668] constexpr std::vector not working as expected
2021-12-12 13:49 [Bug c++/103668] New: constexpr std::vector not working as expected andrei.popa105 at yahoo dot com
` (3 preceding siblings ...)
2021-12-12 18:01 ` andrei.popa105 at yahoo dot com
@ 2021-12-12 20:29 ` redi at gcc dot gnu.org
4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2021-12-12 20:29 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103668
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution|--- |INVALID
--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Ah yes, Jakub is right.
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2021-12-12 20:29 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-12 13:49 [Bug c++/103668] New: constexpr std::vector not working as expected andrei.popa105 at yahoo dot com
2021-12-12 16:40 ` [Bug c++/103668] " redi at gcc dot gnu.org
2021-12-12 16:58 ` andrei.popa105 at yahoo dot com
2021-12-12 17:54 ` jakub at gcc dot gnu.org
2021-12-12 18:01 ` andrei.popa105 at yahoo dot com
2021-12-12 20:29 ` redi at gcc dot gnu.org
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).