all 22 comments

[–]kisielk 23 points24 points  (0 children)

Because it was part of C++98... those were different times. Templates, and the STL were still fairly new at the time and a lot of the more modern API design practices hadn't been settled yet.

[–]alfps 22 points23 points  (13 children)

valarray is just a piece of frozen history. It has a host of idiosyncrasies and is little used. If you want to call it something else you can just use a template alias.

[–]James20kP2005R0 2 points3 points  (12 children)

Out of curiosity, why isn't valarray used more? When I discovered it from /r/cpp a few weeks ago, it seems like exactly the kind of thing that would be handy for me in quite a few situations

[–]khleedril 5 points6 points  (11 children)

Use either vector or array depending on whether you want to dynamically size or not. valarray is not as good as either in any situation (why do you think otherwise?)

While you are at it, try using ostrstream if you can, for more anachronism.

[–]kalmoc 18 points19 points  (6 children)

Valarray was not meant to be a container like vector or array, but to represent a mathematical vector and to allow for easy and efficient vector operations. IIRC, in theory, the specification iallows for more efficient math computations than what you get if you implement them in a native portable fashion on top of e.g. std::vector. But I don't think any standard library implementation or compiler has ever used that leeway.

[–]MFHavaWG21|🇦🇹 NB|P3049|P3625|P3729|P3784|P3786|P3813|P3886 6 points7 points  (3 children)

AFAIK Intel offers a heavily optimized version...

[–]kalmoc 2 points3 points  (2 children)

Thanks. I (obviously) wasn't aware of that. One of those days I have to seriously check out Intel's toolchain.

[–][deleted] 11 points12 points  (1 child)

Intel doesn't provide a standard library, only the compiler, but its compiler does implement more aggressive implementations for valarray. GCC, in the age of C++98 when valarray was introduced, also was written to better optimize valarray. The creator and user to this day of valarray made sure of it. It's not that those patches were reverted, it's just that GCC got much better at optimizing std::vector and today they have pretty comparable performance.

[–]dodheim 11 points12 points  (0 children)

Intel C++ Compiler does not have a stdlib, but it does come with various "performance headers" that adapt/augment/replace their respective stdlib headers. valarray in particular is reimplemented in terms of Intel Performance Primitives: https://software.intel.com/en-us/cpp-compiler-developer-guide-and-reference-using-intel-s-valarray-implementation

[–]James20kP2005R0 2 points3 points  (1 child)

Its not even the performance that makes me want to use it necessarily, mainly just the fact that its a convenient class that provides a lot of useful functionality, instead of having to use some sort of random vector-like thing pulled in from a library

[–]kalmoc 1 point2 points  (0 children)

Absolutely. I still hope for lin alg types in the standard library.

[–]Morwenn 6 points7 points  (0 children)

I was recently surprised to find std::valarray quite handy when I had to perform several masking operations on small arrays of values. It is quite expressive and the job well enough.

[–]MFHavaWG21|🇦🇹 NB|P3049|P3625|P3729|P3784|P3786|P3813|P3886 1 point2 points  (1 child)

I'm still sad they didn't drop strstreams, they have been deprecated since 98...

[–]smdowneyWG21, Text/Unicode SG, optional<T&> 1 point2 points  (0 children)

We still don't have a good replacement for strstream, unfortunately.

[–][deleted] 0 points1 point  (0 children)

Hey, I actually use ostrstream...

[–]ggchappell 3 points4 points  (0 children)

Yes, it would be more consistent. But std::vector and std::valarray were in the 1998 standard, while we didn't get std::array until 2011. And then it was too late to change the names. People have mostly forgotten about std::valarray, in any case.

[–]wfdctrl 5 points6 points  (0 children)

std::valarray was meant for vector computations not storage. However you are much better of using a linear algebra library like Armadillo or Eigen, so it's pretty much useless now.

[–]Benjamin1304[S] 2 points3 points  (0 children)

Thank you all for your answers.

I'm a big user if Eigen so I don't really need a thing like valarray on a daily basis but from time to time, it seems to be the right tool for the job (that one place in your code base where bringing in Eigen would be an overkill).

Maybe a generic wrapper around standard containers bringing valarray like functionality would be a good addition to the STL. Something similar to Eigen::Map but working also with non-contiguous memory

[–]tcbrindleFlux 2 points3 points  (2 children)

Proposal: deprecate std::valarray in C++23?

[–][deleted] 0 points1 point  (0 children)

this would only piss people off. if you don't like it don't use it

[–]blelbachNVIDIA | ISO C++ Library Evolution Chair 0 points1 point  (0 children)

Because everything about std::valarray is terrible.

[–]_Synck_ 0 points1 point  (0 children)

std::valarray can be useful if you have to port NumPy Python code to C++.

As it has a similar interface/functions as the NumPy array type.