Where is it a good idea to use "std::valarray"?

Viewed 2785

I read about std::valarray in a C++ book writen by Nicolai M. Josuttis. He writes in his book The C++ Standard Library, chapter 17.4:

The valarray classes were not designed very well. In fact, nobody tried to determine whether the final specification worked. This happened because nobody felt “responsible” for these classes. The people who introduced valarrays to the C++ standard library left the committee long before the standard was finished. As a consequence, valarrays are rarely used.

So, where is it a good idea to use std::valarray?

1 Answers

Valarrays are created to allow the compiler to make faster executable. However, these optimizations are achievable by other means and valarrays are rarely used.

std::valarray and helper classes are defined to be free of certain forms of aliasing, thus allowing operations on these classes to be optimized similar to the effect of the keyword restrict in the C programming language. In addition, functions and operators that take valarray arguments are allowed to return proxy objects to make it possible for the compiler to optimize an expression such as v1 = av2 + v3; as a single loop that executes v1[i] = av2[i] + v3[i]; avoiding any temporaries or multiple passes. .....

In other words, valarrays are meant to be used in places when there are a lot of values which are passed mostly to simple expressions.

Only rarely are valarrays optimized any further, as in e.g. Intel Parallel Studio.

You can find more information at cppreference

Related