Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between const auto and auto for lambdas

Tags:

c++

Is there any (useful?) difference between:

auto test = [..](..){..};

and

const auto test = [..](..){..};

?

like image 902
Neil Kirk Avatar asked Aug 16 '15 22:08

Neil Kirk


1 Answers

Yes, if the lambda is declared mutable then you cannot call it in the second case.

int x = 0;
const auto test = [x]() mutable { ++x; };
test();  // error
like image 149
Brian Bi Avatar answered Sep 24 '22 07:09

Brian Bi