<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Bogus "reference to local variable error" with lambda in fold expression over comma and assignment"
   href="https://bugs.llvm.org/show_bug.cgi?id=41576">41576</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Bogus "reference to local variable error" with lambda in fold expression over comma and assignment
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++'17
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>vittorio.romeo@outlook.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>blitzrakete@gmail.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The following code

    #include <tuple>

    template <class T>
    struct X
    {
        template <class F>
        void f(F f)
        {
            f(0);
        }
    };

    template <class... Xs>
    void bug(X<Xs>... xs)
    {
        std::tuple<Xs...> t;

        std::apply([&](auto&... ys)
        {   
            (xs.f([&](auto y)
            {
                ys = y;
            }), ...);
        }, t);
    }

    int main()
    {
        bug(X<int>{});
    } 

produces a bogus error with clang++ (version 9.0.0 (trunk 359058))

    <source>:20:10: error: reference to local variable 'xs' declared in
enclosing function 'bug<int>'
            (xs.f([&](auto y)
            ^

The bug can be reproduced on godbolt.org here:
<a href="https://gcc.godbolt.org/z/NNLI5p">https://gcc.godbolt.org/z/NNLI5p</a>

---

Changing the lambda to the following

    std::apply([&xs...](auto&... ys)
    {   
        (xs.f([&ys...](auto y)
        {
            ys = y;
        }), ...);
    }, t);

fixes the error:
<a href="https://gcc.godbolt.org/z/hp2hMN">https://gcc.godbolt.org/z/hp2hMN</a></pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>