public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "milasudril at gmail dot com" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/107862] New: Returning an std::vector from a lambda fails to be constexpr, while a custom class with allocated storage works Date: Thu, 24 Nov 2022 18:39:36 +0000 [thread overview] Message-ID: <bug-107862-4@http.gcc.gnu.org/bugzilla/> (raw) 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.
next reply other threads:[~2022-11-24 18:39 UTC|newest] Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top 2022-11-24 18:39 milasudril at gmail dot com [this message] 2022-11-24 18:49 ` [Bug c++/107862] " pinskia at gcc dot gnu.org
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-107862-4@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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).