Topic: Const overload of valarray::operator[] returns by value


Author: glenlow@pixelglow.com (Glen Low)
Date: Wed, 4 Aug 2004 14:55:10 +0000 (UTC)
Raw View
 > The class template std::valarray<> is supposed to be a concretization
 > of the notion of "array expression".  It particular, it also takes
 > into account other practical constraint as providing access to the
 > contained data.  So while a std::valarray<>::operator[] returns a
 > reference, the type of u + v is not known, therefore there is no
 > guarantee that (u + v)[i] returns an actual reference.  However, it is
 > guaranted that the return value is convertible to a const T&.
 >
 > The above distinction was not clearly made in valarray specification
 > and leads to some serious confusion.

Sorry I'm still confused. In your proposal, is it permissible for (u +
v)[i] to return a (temporary) T (since it is convertible to a const
T&)? If so, what is principly different that u [i] returns a
(temporary) T? Or are you saying that u [i] can also return anything
that is convertible to a const T&?

Cheers,
Glen Low, Pixelglow Software
www.pixelglow.com


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Gabriel Dos Reis <gdr@integrable-solutions.net>
Date: Thu, 5 Aug 2004 14:55:58 +0000 (UTC)
Raw View
glenlow@pixelglow.com (Glen Low) writes:

|  > The class template std::valarray<> is supposed to be a concretization
|  > of the notion of "array expression".  It particular, it also takes
|  > into account other practical constraint as providing access to the
|  > contained data.  So while a std::valarray<>::operator[] returns a
|  > reference, the type of u + v is not known, therefore there is no
|  > guarantee that (u + v)[i] returns an actual reference.  However, it is
|  > guaranted that the return value is convertible to a const T&.
|  >
|  > The above distinction was not clearly made in valarray specification
|  > and leads to some serious confusion.
|
| Sorry I'm still confused. In your proposal, is it permissible for (u +
| v)[i] to return a (temporary) T (since it is convertible to a const
| T&)?

Yes.

| If so, what is principly different that u [i] returns a
| (temporary) T?

If u is of type std::valarray<T>, we have much stronger type
information about its structure.  For example, all its contained
elements are contiguously allocated.  Therefore it makes sense to
return a const T& -- people do want to pass such arrays to
"interfacing" functions.  That type information does not
however hold for general array expression.

| Or are you saying that u [i] can also return anything
| that is convertible to a const T&?

No.

--
                                                        Gabriel Dos Reis
                                            gdr@integrable-solutions.net


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: glenlow@pixelglow.com (Glen Low)
Date: Sun, 1 Aug 2004 15:02:40 +0000 (UTC)
Raw View
Hi all,

Just had a look at C++ Standard Library active issues list #389:

http://www.comeaucomputing.com/iso/lwg-active.html#389

I know valarray has fallen on hard times lately, but I thought having
its operator[] return by value was to facilitate expression template
usage? If it returns by reference as suggested by the issue, surely it
cannot work with an expression template, since that is supposed to be
a temporary?

e.g. suppose valarrays v1 and v2. Then v1 + v2 might be a temporary
expression template, not a valarray. (v1 + v2) [i] could not return a
const reference, since it has no non-temporary object to return?

Cheers,
Glen Low, Pixelglow Software
www.pixelglow.com

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]






Author: Gabriel Dos Reis <gdr@integrable-solutions.net>
Date: Mon, 2 Aug 2004 15:31:10 +0000 (UTC)
Raw View
glenlow@pixelglow.com (Glen Low) writes:

| Hi all,
|
| Just had a look at C++ Standard Library active issues list #389:
|
| http://www.comeaucomputing.com/iso/lwg-active.html#389
|
| I know valarray has fallen on hard times lately, but I thought having
| its operator[] return by value was to facilitate expression template
| usage?

The specification of returning a value is coincidental (or
accidental), to the loosened return-value that allows
expression-template based techniques.

| If it returns by reference as suggested by the issue, surely it
| cannot work with an expression template, since that is supposed to be
| a temporary?

This issue was brought to my attention a year ago, and I failed to
write up a clarification, being short of time.  I'm taking this second
opportunity to write something that can be polished and submitted to
the Library Working Group.

The numerics component of the Standard Library is trying to introduce
two things:

  (1) the _concept_ of *array expression*; and
  (2) a _concrete model_ of the notion of array expression.

An array expression designates a collection of values subject to some
alias-free assumptions. There are various ways to construct array
expressions.  If u and v are array expressions with value-type T, then

  (a) for any unary function f, f(u) is also an array expression;
  (b) for any binary function f, f(u, v) is also an array expressions;
  (c) rules (a) and (b) include operators;
  (d) for any integer i in range, u[i] can be bound to a const T&.
  (e) if u.size() >= v.size() then u[v] is also an array expression.

The class template std::valarray<> is supposed to be a concretization
of the notion of "array expression".  It particular, it also takes
into account other practical constraint as providing access to the
contained data.  So while a std::valarray<>::operator[] returns a
reference, the type of u + v is not known, therefore there is no
guarantee that (u + v)[i] returns an actual reference.  However, it is
guaranted that the return value is convertible to a const T&.

The above distinction was not clearly made in valarray specification
and leads to some serious confusion.

| e.g. suppose valarrays v1 and v2. Then v1 + v2 might be a temporary
| expression template, not a valarray. (v1 + v2) [i] could not return a
| const reference, since it has no non-temporary object to return?

See above.

The wording in the standard needs adjustment.

--
                                                       Gabriel Dos Reis
                                           gdr@integrable-solutions.net


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]