public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "jzwinck at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
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	[thread overview]
Message-ID: <bug-110752-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110752

            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<typename Arg>
    auto & C(Arg && arg) { return arg; }

    void DecltypeWithinLambda()
    {
        int i = 0;
        [[maybe_unused]] auto l = [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 = 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' discards
qualifiers
    | [[maybe_unused]] T t(C(i)); // actual use of C() only matches decltype if
mutable
    |                      ~^~~

If the lambda is marked mutable, it compiles as expected.  So does this, which
seems like it should be equivalent to the above:

    struct F
    {
        void DecltypeWithinConstMethod() const
        {
            using T = decltype(C(i));
            [[maybe_unused]] T t(C(i)); // actual use of C() matches decltype
        }
        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=63192

             reply	other threads:[~2023-07-20 13:57 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-20 13:57 jzwinck at gmail dot com [this message]
2023-11-10 16:12 ` [Bug c++/110752] " ppalka 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-110752-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: link
Be 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).