valarray::operator[]

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ES Kim

    #1

    valarray::operator[]

    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_iterato r<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<typena me T>
    T valarray<T>::op erator[](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


  • E. Robert Tisdale

    #2
    Re: valarray::opera tor[]

    ES Kim wrote:
    [color=blue]
    > 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_iterato r<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<typena me T>
    > T valarray<T>::op erator[](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.[/color]

    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.



    Comment

    Working...