Chapter 6 – C++11 features: Part 1

Auto

auto means that it takes the type of whatever is after the assignment operator. It ignores references and type qualifiers (const volatile).

If we want to take the qualifiers as well, we need to use auto&

Ranged-based for

Ranged-based for is useful when you just want to do something with the elements of a collection, and you do not care about indexes, iterators, or number of elements.

What we cannot do with it?

  • We cannot iterate in reverse
  • There’s no access to the index of the element

Lambda functions

Lambdas are not so different from functions.

Lambda capture list

  • [] take none of the variables from the enclosing scope
  • [&] take all the variables from the enclosing scope by reference
  • [=] take all the variables from the enclosing scope by value

There is also support for capture-by-name [=var1, &var2], to capture only part of the enclosing scope.

We see that the lambda is taken with auto. The type is a std::function, which is a wrapper to a basic function, functor, or lambda.

You may also like...