auto
and decltype
Explained,
by Thomas Becker
about me
The C++14 revision of the Standard adds three new capabilities in
connection with auto and decltype :
Let's discuss each of these in turn. Function return type
deduction means that you can use the 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 int& foo(); decltype(auto) i = foo(); // 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 intWe 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 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 |