<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 - constexpr variable cannot be implicitly captured in C++17 lambda expression"
   href="https://bugs.llvm.org/show_bug.cgi?id=45172">45172</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>constexpr variable cannot be implicitly captured in C++17 lambda expression
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>9.0
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>enhancement
          </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>mali2011@qq.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>// C++ code
static bool lessThan(const float& x, const float& value)
{
        return x < value;
}

int main()
{
        constexpr float THRESHOLD = 1E-6;

        auto compare1 = [](const float& lhs, const float& rhs)
        {
                return (lhs - rhs) <= THRESHOLD;
        };

        auto compare2 = [](const float& lhs, const float& rhs)
        {
                return lessThan(lhs - rhs, THRESHOLD);
        };

        auto compare3 = [](const float& lhs, const float& rhs)
        {
                float THRESHOLD2 = THRESHOLD;
                return (lhs - rhs) <= THRESHOLD2;
        };

        float a = 1.0F, b = 2.0F;
        return compare1(a, b) && compare2(a, b) && compare3(a, b);
}


/************************************************************
when I use clang 6 or clang 9 compiler with C++17 enabled, it complains with
error pasted below. GCC is happy with this. And constexpr variable doesn't need
to capture in C++17 standard.

compare1 passes, compare2 fails, compare3 passes. That's a weird result, When I
move the constexpr variable out of static function call parameter to a local
variable(compare2 => compare3), the compiler doesn't complain anymore.
-------------------------------------------------------------
martin@M2037:~/Desktop/$ clang++ test.cpp -o test -Wall -std=c++17
test.cpp:13:30: error: variable 'THRESHOLD' cannot be implicitly captured in a
lambda with no capture-default specified
                return lessThan(lhs - rhs, THRESHOLD);
                                           ^
test.cpp:9:18: note: 'THRESHOLD' declared here
        constexpr float THRESHOLD = 1E-6;
                        ^
test.cpp:11:18: note: lambda expression begins here
        auto compare1 = [](const float& lhs, const float& rhs)
                        ^
1 error generated.

martin@M2037:~/Desktop/$ clang -v
clang version 9.0.0-2~ubuntu18.04.2 (tags/RELEASE_900/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/8
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.4.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Candidate multilib: x32;@mx32
Selected multilib: .;@m64
************************************************************/</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>