From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 6527C3858C2B; Thu, 20 Jul 2023 13:57:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6527C3858C2B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1689861433; bh=9j7TFZw6Gb/1663l8zP5+MWbax0HCNb1r/koh7F1gRo=; h=From:To:Subject:Date:From; b=lc35rH45rrh7jl/ZeFXpx0T29S2Bk8CQP0GU92MxZ4GiP46RCo2dugxRkzsZCWGZp t7VdyNIo0vfHKhcIn/2RxKneeKYjgd2wxwmac8GdUUICLK86VU+zen9zmyUifq6WTc 3BQAYud+ygb7MHnp32cBnVLA9ICcxn8ILeBeNRh4= From: "jzwinck at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/110752] New: decltype in lambda loses const qualifier unless lambda is mutable Date: Thu, 20 Jul 2023 13:57:13 +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: 13.1.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: jzwinck 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_status bug_severity priority component assigned_to reporter target_milestone 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=3D110752 Bug ID: 110752 Summary: decltype in lambda loses const qualifier unless lambda is mutable Product: gcc Version: 13.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: jzwinck at gmail dot com Target Milestone: --- I believe this code should compile (and with MSVC it does), but with GCC it does not: template auto & C(Arg && arg) { return arg; } void DecltypeWithinLambda() { int i =3D 0; [[maybe_unused]] auto l =3D [i]() // mutable { // does not preserve constness of 'i' within non-mutable lambda // Seems to be evaluating it as if referencing the outer 'i' // Though, decltype((i)) does do the right thing. // alternative solution to mutable is to 'rename' the captured variable list in the capture spec using T =3D decltype(C(i)); [[maybe_unused]] T t(C(i)); // actual use of C() only matches decltype if mutable }; } GCC says the last line is invalid: error: binding reference of type 'T' {aka 'int&'} to 'const int' discar= ds qualifiers | [[maybe_unused]] T t(C(i)); // actual use of C() only matches decltyp= e if mutable | ~^~~ If the lambda is marked mutable, it compiles as expected. So does this, wh= ich seems like it should be equivalent to the above: struct F { void DecltypeWithinConstMethod() const { using T =3D decltype(C(i)); [[maybe_unused]] T t(C(i)); // actual use of C() matches declty= pe } int i; }; Demo: https://gcc.godbolt.org/z/PzEodY1bW A somewhat similar bug, but not the same: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D63192=