| Summary: | Lambda capture by reference of const reference fails | ||
|---|---|---|---|
| Product: | gcc | Reporter: | Nik Bougalis <nikb> |
| Component: | c++ | Assignee: | Not yet assigned to anyone <unassigned> |
| Status: | RESOLVED DUPLICATE | ||
| Severity: | normal | CC: | daniel.kruegler, msebor, nathan, webrown.cpp |
| Priority: | P3 | Keywords: | c++-lambda, rejects-valid |
| Version: | 6.0 | ||
| Target Milestone: | --- | ||
| Host: | Target: | ||
| Build: | Known to work: | ||
| Known to fail: | 5.3.0, 6.1.0, 7.0 | Last reconfirmed: | 2016-07-01 00:00:00 |
| Bug Depends on: | |||
| Bug Blocks: | 54367 | ||
I can confirm this with: gcc version 5.3.1 20160329 (GCC) and gcc version 6.0.0 20160403 (experimental) (GCC). It also fails with x86 gcc 6.1 (g++ (DRW-internal-build) 6.1.0) on gcc.godbolt.org Confirmed with today's top of trunk (7.0). The code never seems to have been accepted.
$ cat xxx.c && /build/gcc-trunk-svn/gcc/xgcc -B /build/gcc-trunk-svn/gcc -S -Wall -Wextra -Wpedantic -xc++ xxx.c
int const i = 0;
auto test = [&j = i]() { };
xxx.c:2:13: error: binding ‘const int’ to reference of type ‘int&’ discards qualifiers
auto test = [&j = i]() { };
^
|
Consider the following minimal MCVE: const int& test() { int const static i = 0; return i; } int main() { auto square = [&j = test()]() { }; return 0; } The expectation here is that `pays` will be a `const int&`. Clang successfully compiles this code (along with a warning about j being unused) but gcc fails to compile with an interesting error: error: binding 'const int' to reference of type 'int&' discards qualifiers &j = test() ~~~~~^~ This is clearly wrong. I believe that the correct behavior would be for `j` to be a `const int&`. We can simplify the example a bit more. The following code successfully compiles and appears to do the "right" thing on both clang and gcc: int main() { int const i = 0; auto test = [&i]() { }; test(); return 0; } The following code compiles and does the "right" thing in clang, but not in gcc, where compilation fails: int main() { int const i = 0; auto test = [&j = i]() { }; test(); return 0; }