This code fails to compile: int main() { int x = 0; auto l = [&rx = static_cast<const int&>(x)]() {}; } The error message is: test.cpp:3:14: error: binding 'const int' to reference of type 'int&' discards qualifiers auto l = [&rx = static_cast<const int&>(x)]() { But according to [expr.prim.lambda]/11 rx should be captured as auto &rx = static_cast<const int&>(x), that is as const int&. Tested on MacPorts g++-mp-5: $ g++-mp-5 -v Using built-in specs. COLLECT_GCC=g++-mp-5 ... gcc version 5.1.0 (MacPorts gcc5 5.1.0_1)
I'm also seeing the same problem, and another very similar issue: int main() { const int x = 0; auto l = [&rx = x]() {}; } This is rejected by g++ (4.9.3 and 5.2.0) but accepted by clang. Also discussed on StackOverflow: http://stackoverflow.com/q/31919260/146041
Confirmed.
As the standardization of `std::as_const`, this bug becomes a blocker.
int const x = 5; void Foo () { auto l = [&rx = x]() {}; }
*** Bug 70385 has been marked as a duplicate of this bug. ***
Author: nathan Date: Wed Jan 4 15:23:40 2017 New Revision: 244056 URL: https://gcc.gnu.org/viewcvs?rev=244056&root=gcc&view=rev Log: cp/ PR c++/66735 * cp-tree.h (DECLTYPE_FOR_REF_CAPTURE): New. (lambda_capture_field_type): Update prototype. * lambda.c (lambda_capture_field_type): Add is_reference parm. Add referenceness here. (add_capture): Adjust lambda_capture_field_type call, refactor error checking. * pt.c (tsubst): Adjust lambda_capture_field_type call. testsuite/ PR c++/66735 * g++.dg/cpp1y/pr66735.C: New. Added: trunk/gcc/testsuite/g++.dg/cpp1y/pr66735.C Modified: trunk/gcc/cp/ChangeLog trunk/gcc/cp/cp-tree.h trunk/gcc/cp/lambda.c trunk/gcc/cp/pt.c trunk/gcc/testsuite/ChangeLog
Fixed trunk r244056.