auto and expression templates

suggest change

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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


auto keyword:
* auto
* auto and expression templates

Table Of Contents
8 Arrays
11 Loops
16 auto keyword
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype