public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/107862] New: Returning an std::vector from a lambda fails to be constexpr, while a custom class with allocated storage works
@ 2022-11-24 18:39 milasudril at gmail dot com
2022-11-24 18:49 ` [Bug c++/107862] " pinskia at gcc dot gnu.org
0 siblings, 1 reply; 2+ messages in thread
From: milasudril at gmail dot com @ 2022-11-24 18:39 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107862
Bug ID: 107862
Summary: Returning an std::vector from a lambda fails to be
constexpr, while a custom class with allocated storage
works
Product: gcc
Version: 12.2.0
URL: https://godbolt.org/z/dve3Yx8ax
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: milasudril at gmail dot com
Target Milestone: ---
Host: x86-64_linux_gnu
Target: x86-64_linux_gnu
Minimal working example:
#include <array>
#include <algorithm>
#include <vector>
struct Test {
using value_type = int;
constexpr Test(){ data = new value_type(0); }
constexpr ~Test(){ delete data; }
constexpr unsigned size() const { return 1; }
constexpr auto begin() const { return data; }
constexpr auto end() const { return data + 1; }
private:
value_type* data;
};
consteval auto dynamic_data_to_array(auto generator) {
auto test = generator();
using VT = typename decltype(test)::value_type;
std::array<VT, test.size()> data;
std::copy(test.begin(), test.end(), data.begin());
return data;
}
constexpr Test get_test() { return Test(); }
int main()
{
// Does not work constexpr auto data0 = dynamic_data_to_array([] { return
std::vector<int>{0};});
constexpr auto data1 = dynamic_data_to_array([] { return Test{};});
return data1[0];
}
It works to return a Test object here, but not an std::vector. Compiler output:
<source>:22:29: in 'constexpr' expansion of 'test.std::vector<int>::size()'
<source>:22:33: error: the value of 'test' is not usable in a constant
expression
22 | std::array<VT, test.size()> data;
| ^~~~
<source>:19:10: note: 'test' was not declared 'constexpr'
19 | auto test = generator();
| ^~~~
<source>:22:29: note: in template argument for type 'long unsigned int'
22 | std::array<VT, test.size()> data;
| ~~~~~~~~~^~
<source>:22:29: in 'constexpr' expansion of 'test.std::vector<int>::size()'
<source>:22:33: error: the value of 'test' is not usable in a constant
expression
22 | std::array<VT, test.size()> data;
| ^~~~
<source>:19:10: note: 'test' was not declared 'constexpr'
19 | auto test = generator();
| ^~~~
<source>:22:29: note: in template argument for type 'long unsigned int'
22 | std::array<VT, test.size()> data;
I am not sure if this is by the standard, or if this is related to some missing
implementation details in the standard library. Note: clang doesn't like it
either.
^ permalink raw reply [flat|nested] 2+ messages in thread
* [Bug c++/107862] Returning an std::vector from a lambda fails to be constexpr, while a custom class with allocated storage works
2022-11-24 18:39 [Bug c++/107862] New: Returning an std::vector from a lambda fails to be constexpr, while a custom class with allocated storage works milasudril at gmail dot com
@ 2022-11-24 18:49 ` pinskia at gcc dot gnu.org
0 siblings, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-24 18:49 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107862
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I don't think this is valid code.
In the function dynamic_data_to_array you have:
std::array<VT, test.size()> data;
But test.size() is not a constexpr unless test is a constexpr. making test a
constexpr does not work as it has dynamic allocation
In your Test struct you have:
constexpr unsigned size() const { return 1; }
So it is always a constexpr ...
Note clang also rejects this code for the same reason as GCC even with libc++.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-11-24 18:49 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-24 18:39 [Bug c++/107862] New: Returning an std::vector from a lambda fails to be constexpr, while a custom class with allocated storage works milasudril at gmail dot com
2022-11-24 18:49 ` [Bug c++/107862] " pinskia 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).