Appendix E. valarray and Matrix Operations

As mentioned in Chapter 8, a Standard Library container class separate from the STL, std::valarray, dates back to C++98. It supports arithmetic operators and provides “for numeric computation when runtime efficiency is more important than flexibility with respect to operations and element types.”1 With slice and stride functions also accompanying the valarray class, it can also facilitate representation of arrays of higher dimensions—in particular, a (two-dimensional) matrix.

While valarray has these very useful properties that would seem to make it an obvious choice for matrix math, it has played to mixed reviews. This reaction dates back to its original specification, which was never fully complete because of debates over whether to require implementation using expression templates, a new technique at the time that could significantly optimize performance. In the end, this was not mandated. As a result, “initial implementations were slow, and thus users did not want to rely on [them].”2

More about the history of valarray is discussed in Section 27.4 of C++ Templates: The Complete Guide, 2nd edition, by David Vandevoorde et al. (Addison-Wesley, 2017). You can also find additional details about valarray in Section S.2 of the online supplemental chapter of The C++ Standard Library: A Tutorial and Reference, 2nd edition, by Nicolai M. Josuttis (Addison-Wesley, 2012).

At the time of this writing, however, two of the mainstream Standard Library ...

Get Learning Modern C++ for Finance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.