https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95269
Bug ID: 95269 Summary: Lambda is allowed to capture any constexpr variable without specifying any captures Product: gcc Version: 10.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: dragondreamer at live dot com Target Milestone: --- G++ (at least these versions: 8.2.1, 9.2.1, 10.1.0, trunk) successfully compiles the following code: // =================================== template<typename Lambda> constexpr void test(Lambda lambda) { constexpr auto result = lambda(); static_assert(result.value == 1); } struct teststruct { int value = 0; }; constexpr auto get_value() { teststruct result{}; result.value = 1; return result; } int main() { constexpr auto value = get_value(); //Here's a lambda without any captures auto options_lambda = []() { return value; }; test(options_lambda); } // =================================== This code instead should produce an error when trying to create a lambda which uses "value" without capturing it. This is the exact behavior of Clang and MSVC, which both produce an error message. As far as I understand, the following rule must be applied here: "If a lambda expression (or an instantiation of a generic lambda's function call operator) ODR-uses this or any variable with automatic storage duration, it must be captured by the lambda expression."