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:



Table Of Contents