[llvm-bugs] [Bug 45172] New: constexpr variable cannot be implicitly captured in C++17 lambda expression
via llvm-bugs
llvm-bugs at lists.llvm.org
Tue Mar 10 19:49:17 PDT 2020
https://bugs.llvm.org/show_bug.cgi?id=45172
Bug ID: 45172
Summary: constexpr variable cannot be implicitly captured in
C++17 lambda expression
Product: clang
Version: 9.0
Hardware: PC
OS: Linux
Status: NEW
Severity: enhancement
Priority: P
Component: C++17
Assignee: unassignedclangbugs at nondot.org
Reporter: mali2011 at qq.com
CC: blitzrakete at gmail.com, erik.pilkington at gmail.com,
llvm-bugs at lists.llvm.org, richard-llvm at metafoo.co.uk
// 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 at 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 at 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
************************************************************/
--
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/20200311/f360be15/attachment.html>
More information about the llvm-bugs
mailing list