auto
can also cause problems where expression templates come into play:
auto mult(int c) {
return c * std::valarray<int>{1};
}
auto v = mult(3);
std::cout << v[0]; // some value that could be, but almost certainly is not, 3.
The reason is that operator*
on valarray
gives you a proxy object that refers to the valarray
as a means of lazy evaluation. By using auto
, you're creating a dangling reference. Instead of mult
had returned a std::valarray<int>
, then the code would definitely print 3.