By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,562 Members | 1,282 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,562 IT Pros & Developers. It's quick & easy.

valarray::operator[]

P: n/a
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>::operator[](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.

--
ES Kim
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
ES Kim wrote:
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>::operator[](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.

Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.