CodexBloom - Programming Q&A Platform

implementing noexcept and lambda captures in C++20 leading to ambiguous overloads

👀 Views: 203 đŸ’Ŧ Answers: 1 📅 Created: 2025-08-23
c++20 noexcept lambdas C++

I'm getting frustrated with I'm performance testing and I'm relatively new to this, so bear with me. I'm working with a puzzling compiler behavior related to `noexcept` and lambda captures in C++20. I have a lambda that captures some local variables and returns a value based on those captures, but when I mark it with `noexcept`, the compiler throws an ambiguous overload behavior. Here's a simplified version of my code: ```cpp #include <iostream> #include <vector> void process(std::vector<int>& vec) { auto lambda = [&, i = 0]() noexcept { return vec[i++]; }; for (int j = 0; j < vec.size(); ++j) { std::cout << lambda() << " "; } } int main() { std::vector<int> values = {1, 2, 3, 4, 5}; process(values); return 0; } ``` When compiling, I get the following behavior: ``` behavior: call to 'lambda' is ambiguous ``` I've tried removing `noexcept`, and the code compiles fine, but I'm trying to understand how `noexcept` interacts with captured variables and if there's a specific reason for this ambiguity. I'm using GCC 10.2.0. Has anyone else faced this scenario, or can someone shed light on how to resolve it? Is there a best practice regarding the use of `noexcept` with lambdas that capture local variables? For context: I'm using C++ on Linux. Is there a better approach? Is there a better approach? Thanks for any help you can provide! This is for a mobile app running on CentOS. What's the correct way to implement this? I'm developing on Ubuntu 20.04 with C++. Could someone point me to the right documentation?