You invested your money. Find out what happened.
GreaterThanZero.com


Page 10 of: C++ auto and decltype Explained, by Thomas Becker   about me  

New in C++14

The C++14 revision of the Standard adds three new capabilities in connection with auto and decltype:
  1. Function return type deduction
  2. Alternate type deduction when declaring a variable
  3. Type deduction for lambda arguments

Let's discuss each of these in turn. Function return type deduction means that you can use the auto keyword where you would usually place the result type of a function. You can now write

auto add(int a, int b) {
  return a + b;
}
instead of
int add(int a, int b) {
  return a + b;
}

Needless to say, there are some issues arising from things such as multiple return statements, recursive calls, and forward declarations, but none of these are very serious. See e.g. the Wikipedia article for details.

Up next, let's take a look at alternate type deduction when declaring a variable. Recall that the way in which auto and decltype deduce the type of an expression are quite different. In C++14, we have the option to employ the type deduction rules used by decltype when initializing a variable:

int& foo();
decltype(auto) i = f(); // i is int&
Had we used just auto here, we would have ended up with an int instead of an int&:
int& foo();
auto i = f(); // i is int
We could of course also have arrived at an int& by augmenting the auto keyword with a reference:
int& foo();
auto& i = f(); // i is int&

Finally, type deduction for lambda arguments means that you can now use the keyword auto in lieu of an actual type when you declare a lambda function parameter, as in

[](auto x, auto y) {return x + y;};

What we're looking at here is, of course, a generic lambda. So one coud say that classes and functions use the template syntax to achieve genericity, while lambdas use the auto keyword for that purpose.