I tried to increment a local variable from 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;
}
But got the following error:
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 some workaround about that in c++11/14?
Aucun commentaire:
Enregistrer un commentaire