[llvm-bugs] [Bug 49922] New: By-reference variadic init-capture copies objects in lambda

via llvm-bugs llvm-bugs at lists.llvm.org
Sun Apr 11 06:10:55 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=49922

            Bug ID: 49922
           Summary: By-reference variadic init-capture copies objects in
                    lambda
           Product: clang
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++2a
          Assignee: unassignedclangbugs at nondot.org
          Reporter: andrey458641387 at gmail.com
                CC: blitzrakete at gmail.com, erik.pilkington at gmail.com,
                    llvm-bugs at lists.llvm.org, richard-llvm at metafoo.co.uk

Overview:
In a by-reference capture with an initializer that is a pack expansion (of the
form "&... identifier initializer") the result of the initializer is stored in
the lambda object by-copy instead of by-reference.

Steps to reproduce: build and run the following code in C++20 mode.

    void print_arg_addresses(auto &...args) {
        (std::cout << ... << static_cast<const void *>(&args)) << '\n';
    }

    void pass_arg_refs_through_lambda(auto &...args) {
        [&...args = args] { // <-- THE BUG IS HERE!
            print_arg_addresses(args...);
        }();
    }

    int main() {
        auto a = 0;
        print_arg_addresses(a);
        pass_arg_refs_through_lambda(a);
    }

Here is a Compiler Explorer link with more variants of the code:
https://godbolt.org/z/Ycaejs9jd

Actual Results: the printed addresses are NOT equal.
Expected Results: the printed addresses are equal.

This bug is reproducible on all versions of Clang starting with 9.0 (when the
feature was first implemented) and including the "trunk" version from Compiler
Explorer. It has been reproduced on both Linux and Windows (with the clang-cl
driver).

GCC and MSVC both compile the code as expected - capture by-reference (MSVC
requires an explicit "template<typename... Args>" instead of just "auto &..."
though).

Also, normal by-reference captures with initializers (i.e. "&arg = arg") work
correctly (see CE example).
Optimization flags don't appear to affect the behavior.

<speculation>
Judging by the behavior of some real-world code, the ref-qualifier is simply
ignored, and the capture is treated as if it was "by-copy".
</speculation>

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210411/f71f683c/attachment.html>


More information about the llvm-bugs mailing list