I tried to increment a local variable from a lambda expression:
#include <iostream> template<typename T> T foo(T t){ T temp{}; [temp]() -> void { temp++; }(); return temp; } int main() { std::cout<< foo(10) << std::endl; }
Demo
But the following error turned out:
main.cpp: In instantiation of 'foo(T)::<lambda()> [with T = int]': main.cpp:6:6: required from 'struct foo(T) [with T = int]::<lambda()>' main.cpp:8:6: required from 'T foo(T) [with T = int]' main.cpp:14:23: required from here main.cpp:7:13: error: increment of read-only variable 'temp' temp++; ^
Is there a workaround for this in C ++ 11/14?
c ++ lambda c ++ 11 c ++ 14
St. Antario
source share