GCC 13.1.0 Generates Incorrect Code for C++ Lambdas Capturing By Reference
I'm building a feature where Hey everyone, I'm running into an issue that's driving me crazy... I'm experiencing an scenario with GCC 13.1.0 where lambdas capturing variables by reference seem to produce incorrect behavior during runtime. The following code snippet demonstrates the question: ```cpp #include <iostream> #include <vector> void modifyVector(std::vector<int>& vec) { for (auto& val : vec) { auto increment = [&val]() { val++; }; increment(); } } int main() { std::vector<int> numbers = {1, 2, 3}; modifyVector(numbers); for (const auto& num : numbers) { std::cout << num << " "; // Expected output: 2 3 4 } return 0; } ``` Instead of incrementing each value as expected, the output I'm getting is `1 2 3` when I compile it with `g++ -std=c++11 -o test test.cpp`. I've tried various optimization flags such as `-O1`, `-O2`, and even `-O3`, but the behavior remains the same. I also tested the code on Clang and MSVC, and they produced the expected output. Could this be a bug in GCC or a question with how Iām using lambdas? Any insights or workarounds would be greatly appreciated. This is for a application running on Windows 10. Any examples would be super helpful. I'm developing on Linux with C++. What am I doing wrong? I'm on Windows 11 using the latest version of C++.