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
?
std:: valarray class in C++ C++98 introduced a special container called valarray to hold and provide mathematical operations on arrays efficiently. It supports element-wise mathematical operations and various forms of generalized subscript operators, slicing and indirect access.
Syntax : slice( std::size_t start, std::size_t size, std::size_t stride ); size_t star : is the index of the first element in the selection size_t size : is the number of elements in the selection stride : is the span that separates the elements selected.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With