Yes, this has been asked before, and the answer has been:
valarray
s (value arrays) are intended to bring some of the speed of Fortran to C++. You wouldn't make avalarray
of pointers so the compiler can make assumptions about the code and optimise it better. (The main reason that Fortran is so fast is that there is no pointer type so there can be no pointer aliasing.)
or:
valarray
is also supposed to eliminate any possibility of aliasing [...]
But these answers make no sense to me.
valarray
and vector
are class templates, and as such, they don't even exist until instantiated.
And of course, a vector<int>
doesn't cause aliasing issues any more than valarray<int>
does.
Given this, what was the purpose of valarray
, and why did they not simply put the same functionality into vector
instead?
valarray has the slice mechanism
valarray is expected to be implemented using expression template for its numerical operators
Separation of concern? A
vector
and avalarray
solve different problems. Quoting from the standard, avector
is a (§23.3.6.1 [vector.overview] p1
)while a
valarray
is a (§26.6.2.1 [template.valarray.overview] p1
)As you can see, they serve different purposes. A
vector
is a generalized dynamic array, while avalarray
represents a set of values. It's also not resizeable and only assignable.