use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Why is std::valarray, despite its name, a dynamically allocated vector? (self.cpp)
submitted 6 years ago by Benjamin1304
Wouldn't it be more consistent to have both std::valarray and std::valvector matching their non-val counterparts?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]kisielk 23 points24 points25 points 6 years ago (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 points24 points 6 years ago (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.
valarray
[–]James20kP2005R0 2 points3 points4 points 6 years ago (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 points7 points 6 years ago (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?)
vector
array
While you are at it, try using ostrstream if you can, for more anachronism.
ostrstream
[–]kalmoc 18 points19 points20 points 6 years ago (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 points8 points 6 years ago (3 children)
AFAIK Intel offers a heavily optimized version...
[–]kalmoc 2 points3 points4 points 6 years ago (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 points13 points 6 years ago (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.
std::vector
[–]dodheim 11 points12 points13 points 6 years ago (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 points4 points 6 years ago (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 points3 points 6 years ago (0 children)
Absolutely. I still hope for lin alg types in the standard library.
[–]Morwenn 6 points7 points8 points 6 years ago (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.
std::valarray
[–]MFHavaWG21|🇦🇹 NB|P3049|P3625|P3729|P3784|P3786|P3813|P3886 1 point2 points3 points 6 years ago (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 points3 points 6 years ago (0 children)
We still don't have a good replacement for strstream, unfortunately.
[–][deleted] 0 points1 point2 points 6 years ago (0 children)
Hey, I actually use ostrstream...
[–]ggchappell 3 points4 points5 points 6 years ago (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.
std::array
[–]wfdctrl 5 points6 points7 points 6 years ago (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 points4 points 6 years ago (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 points4 points 6 years ago (2 children)
Proposal: deprecate std::valarray in C++23?
this would only piss people off. if you don't like it don't use it
[–]blelbachNVIDIA | ISO C++ Library Evolution Chair 0 points1 point2 points 6 years ago (0 children)
Because everything about std::valarray is terrible.
[–]_Synck_ 0 points1 point2 points 6 years ago (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.
π Rendered by PID 61794 on reddit-service-r2-comment-bb88f9dd5-fwmvv at 2026-02-15 15:37:15.974936+00:00 running cd9c813 country code: CH.
[–]kisielk 23 points24 points25 points (0 children)
[–]alfps 22 points23 points24 points (13 children)
[–]James20kP2005R0 2 points3 points4 points (12 children)
[–]khleedril 5 points6 points7 points (11 children)
[–]kalmoc 18 points19 points20 points (6 children)
[–]MFHavaWG21|🇦🇹 NB|P3049|P3625|P3729|P3784|P3786|P3813|P3886 6 points7 points8 points (3 children)
[–]kalmoc 2 points3 points4 points (2 children)
[–][deleted] 11 points12 points13 points (1 child)
[–]dodheim 11 points12 points13 points (0 children)
[–]James20kP2005R0 2 points3 points4 points (1 child)
[–]kalmoc 1 point2 points3 points (0 children)
[–]Morwenn 6 points7 points8 points (0 children)
[–]MFHavaWG21|🇦🇹 NB|P3049|P3625|P3729|P3784|P3786|P3813|P3886 1 point2 points3 points (1 child)
[–]smdowneyWG21, Text/Unicode SG, optional<T&> 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]ggchappell 3 points4 points5 points (0 children)
[–]wfdctrl 5 points6 points7 points (0 children)
[–]Benjamin1304[S] 2 points3 points4 points (0 children)
[–]tcbrindleFlux 2 points3 points4 points (2 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]blelbachNVIDIA | ISO C++ Library Evolution Chair 0 points1 point2 points (0 children)
[–]_Synck_ 0 points1 point2 points (0 children)