From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id E79C4382FADD; Thu, 24 Nov 2022 18:39:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E79C4382FADD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1669315176; bh=G8RGGk+5sYiyjF6dzo+XYDVxknNwtHMICXMqUsXOAfQ=; h=From:To:Subject:Date:From; b=rOcf2xnIjwYqWkwDGtWy6y9k2LPHyeO/jIRStsgy4srZqgHTLx+sXoIfjO9Bnuu3n sjTX2qqcLovz3e6nkLrXeYv6uWuyWU6TxbNEnoY/8R4ezJbWGN89uK+lEN4fBFCjfy 0AfuuyLyBMgrvv91sC3Y0flA4xHrMQmDNS/UgV4E= From: "milasudril at gmail dot com" 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 12.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: milasudril at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_file_loc bug_status bug_severity priority component assigned_to reporter target_milestone cf_gcchost cf_gcctarget Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D107862 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 #include #include struct Test { using value_type =3D int; constexpr Test(){ data =3D 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 =3D generator(); using VT =3D typename decltype(test)::value_type; std::array 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 =3D dynamic_data_to_array([] { return std::vector{0};}); constexpr auto data1 =3D dynamic_data_to_array([] { return Test{};}); return data1[0]; } It works to return a Test object here, but not an std::vector. Compiler out= put: :22:29: in 'constexpr' expansion of 'test.std::vector::size(= )' :22:33: error: the value of 'test' is not usable in a constant expression 22 | std::array data; | ^~~~ :19:10: note: 'test' was not declared 'constexpr' 19 | auto test =3D generator(); | ^~~~ :22:29: note: in template argument for type 'long unsigned int' 22 | std::array data; | ~~~~~~~~~^~ :22:29: in 'constexpr' expansion of 'test.std::vector::size(= )' :22:33: error: the value of 'test' is not usable in a constant expression 22 | std::array data; | ^~~~ :19:10: note: 'test' was not declared 'constexpr' 19 | auto test =3D generator(); | ^~~~ :22:29: note: in template argument for type 'long unsigned int' 22 | std::array data; I am not sure if this is by the standard, or if this is related to some mis= sing implementation details in the standard library. Note: clang doesn't like it either.=