valarray::operator[]

E

ES Kim

comp.std.c++ would be a better place for this question.
Forgive me, but I can't post anything on moderated newsgroups
for some reason.

valarray doesn't have iterators of its own, which makes functions
in <algorithm> less useful. &v[0] works like an iterator, but even this
trick doesn't work for const valarray's.

void f(const valarray<double>& v)
{
copy(&v[0], &v[v.size()], ostream_iterator<int>(cout, " "));
}

This code doesn't compile (and it looks ugly, I'd say) since you can't
take the address of an r-value:

template<typename T>
T valarray<T>::eek:perator[](size_t) const;

I'm curious the reason why the return type is T instead of const T& ?
I know valarray's are not meant for an ordinary container, but I think
there would be nothing to lose for taking advantage of const-correctness.
 
E

E. Robert Tisdale

ES said:
comp.std.c++ would be a better place for this question.
Forgive me, but I can't post anything on moderated newsgroups
for some reason.

valarray doesn't have iterators of its own, which makes functions
in <algorithm> less useful. &v[0] works like an iterator,
but even this trick doesn't work for const valarray's.

void f(const valarray<double>& v) {
copy(&v[0], &v[v.size()], ostream_iterator<int>(cout, " "));
}

This code doesn't compile (and it looks ugly, I'd say)
since you can't take the address of an r-value:

template<typename T>
T valarray<T>::eek:perator[](size_t) const;

I'm curious the reason why the return type is T instead of const T&?
I know valarray's are not meant for an ordinary container,
but I think there would be nothing to lose
for taking advantage of const-correctness.

valarray is a numerical class library
which probably doesn't really belong in the standard library.
I believe that Kent Budge was thinking that,
if valarray was part of the standard library,
C++ compiler developers might be encouraged
to treat a valarray as a built-in type
and aggressively optimize on them
for fast vector processing super computers.
Unfortunately, C++ compiler developers
couldn't be convinced to invest in optimized compilers
for people that were going to use Fortran 90 anyway.

Iteration is evil as far as numerical computing is concerned.
It virtually precludes any kind of parallelization.

valarray really wasn't meant to be generic.
T really must be a real or complex floating-point number.
valarray is also paranoid about aliases.
Return by value avoids creating an alias
for part of a valarray.

valarrays probably should never appear
in the body of your program.
They are really best used as private members
of a vector and/or matrix class library.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top