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; }
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]() { }; ^
Same as 66735, to which I have attached a simpler testcase *** This bug has been marked as a duplicate of bug 66735 ***