public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "arthur.j.odwyer at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/94376] New: When nested inside a lambda body, [=] captures by const value instead of by value
Date: Sat, 28 Mar 2020 00:14:20 +0000	[thread overview]
Message-ID: <bug-94376-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 94376
           Summary: When nested inside a lambda body, [=] captures by
                    const value instead of by value
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: arthur.j.odwyer at gmail dot com
  Target Milestone: ---

David Blaikie, Richard Smith, and I discovered this GCC bug while tracking down
a separate bug in llvm::function_ref whose constructor template was improperly
SFINAEd so that construction from `const T&&` was done wrong. A GCC bug caused
construction from `const T&&` to happen on GCC but not on Clang or EDG. Here's
the reduced test case:

// https://godbolt.org/z/oCvLpv
#include <stdio.h>
#include <utility>

struct I {
    I() { puts(__PRETTY_FUNCTION__); }
    I(I&) { puts(__PRETTY_FUNCTION__); }
    I(const I&) { puts(__PRETTY_FUNCTION__); }
    I(I&&) { puts(__PRETTY_FUNCTION__); }
    I(const I&&) { puts(__PRETTY_FUNCTION__); }

    void operator++() const {}
};

int main() {
    I i;
    auto one = [=]() { 
        return [=]() {
            ++i;
        };
    }();
    puts("-----");
    auto two = std::move(one);  // !!
}

On the line marked "!!", one's implicitly generated move-constructor calls
`I(const I&&)` rather than `I(I&&)` to move the captured copy of `i`. It does
this because it has improperly decided that the type of the captured copy of
`i` should be `const I` instead of plain old `I`.

Richard Smith writes:
> [expr.prim.lambda.capture]p10 is the relevant rule:
> "The type of such a data member is the referenced type
> if the entity is a reference to an object, an lvalue reference
> to the referenced function type if the entity is a reference to a function,
> or the type of the corresponding captured entity otherwise."
>
> Regardless of whether you think the captured entity is
> the original variable or the member of the outer closure type,
> the type of that entity is not const-qualified.
> So the inner capture should not have a const-qualified type.

Besides exposing bugs in llvm::function_ref (a good effect!), GCC's
implementation divergence here could have the bad effect of causing additional
expensive copies when lambdas with improperly const-qualified captures are
moved around. Example:
https://godbolt.org/z/LWEF47


Bug 86697 might be related:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86697

             reply	other threads:[~2020-03-28  0:14 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-28  0:14 arthur.j.odwyer at gmail dot com [this message]
2021-11-02 17:27 ` [Bug c++/94376] " ppalka at gcc dot gnu.org
2021-11-19 13:54 ` cvs-commit at gcc dot gnu.org
2021-11-19 13:56 ` ppalka at gcc dot gnu.org
2021-12-19 19:42 ` cvs-commit at gcc dot gnu.org
2021-12-28 10:01 ` pinskia at gcc dot gnu.org
2022-04-26 20:26 ` 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-94376-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).