Using auto in a lambda function

auto keyword does not work as a type for function arguments, in C++11. If you don’t want to use the actual type in lambda functions, then you could use the code below. for_each(begin(v), end(v), [](decltype(*begin(v)) it ){ foo( it + 5); }); The code in the question works just fine in C++ 14.

Using auto in loops c++

A range based loop could be a cleaner solution: for (const auto& i : a) { } Here, i is a const reference to an element of container a. Otherwise, if you need the index, or if you don’t want to loop over the entire range, you can get the type with decltype(a.size()). for (decltype(a.size()) … Read more

Usage of auto in C++11

The difference is that in the first case auto is deduced to int* while in the second case auto is deduced to int, which results in both p1 and p2 being of type int*. The type deduction mechanism for auto is equivalent to that of template arguments. The type deduction in your example is therefore … Read more