C++11 - Auto

Using auto to declare variables in C++11 relieves the developer of so many troubles -

1. Initializer required
int a;  //variables can be left initialized
auto b = 1;  //auto variables need to be initialized

2. Work with types known after templates have been initialized
template<typename It> void f1(It a, Itb)
{
    while(b != a)
    {
        auto deref = *b;
        //do something with the dereferenced iterator b
     }
}

3. Work with types known only to compilers
In the lambda for comparison of two TypeA objects pointed to by std::unique_ptr's -
auto deref = [](const std::unique_ptr<TypeA> & a1, const std::unique_ptr<TypeA & a2) { return *a1 < *a2; }
In C++14 parameters of lambda expressions -
auto deref = [](const auto & a1, const auto & a2) { return *a1 < *a2; }

4. Work with types not usually known to developers
std::vector<int> v;
//...
unsigned size = v.size();
This is incorrect, return type is std::vector<int>::size_type which is the same size as unsigned int on 32-bit system but different on a 64-bit system.

There are some downfalls of using auto, Meyers shows an example with Proxy Classes.

For example there is a function f -
  std::vector<bool> f();
which when invoked as -
  bool b = f()[0];
the value of b is populated as expected.

However when it is invoked as -
  auto a = f()[0];
the result is undefined. The std::vector<bool> returned by f() is a temporary. this temporary object has the operator[] invoked on it, which returns a proxy class i.e. std::vector<bool>::reference. This proxy class is provided by the standard, it converts implicitly to bool but in this case since 'a' is auto its type is not bool instead it is an object of type std::vector<bool>::reference. However since the return of f() is a temporary object, the index operator returns a reference on the temporary and thus it is undefined after the execution of this statement is complete.

To avoid this problem of invisible proxy classes, use static_cast i.e. -
  auto a = static_cast<bool>(f()[0]);