Movatterモバイル変換


[0]ホーム

URL:



This page is a snapshot from the LWG issues list, see theLibrary Active Issues List for more information and the meaning ofCD1 status.

389. Const overload of valarray::operator[] returns by value

Section: 29.6.2.4[valarray.access]Status:CD1Submitter: Gabriel Dos ReisOpened: 2002-11-08Last modified: 2016-01-28

Priority:Not Prioritized

View all otherissues in [valarray.access].

View all issues withCD1 status.

Duplicate of:77

Discussion:

Consider the following program:

    #include <iostream>    #include <ostream>    #include <vector>    #include <valarray>    #include <algorithm>    #include <iterator>    template<typename Array>    void print(const Array& a)    {    using namespace std;    typedef typename Array::value_type T;    copy(&a[0], &a[0] + a.size(),    ostream_iterator<T>(std::cout, " "));    }    template<typename T, unsigned N>    unsigned size(T(&)[N]) { return N; }    int main()    {    double array[] = { 0.89, 9.3, 7, 6.23 };    std::vector<double> v(array, array + size(array));    std::valarray<double> w(array, size(array));    print(v); // #1    std::cout << std::endl;    print(w); // #2    std::cout << std::endl;    }

While the call numbered #1 succeeds, the call numbered #2 failsbecause the const version of the member functionvalarray<T>::operator[](size_t) returns a value instead of aconst-reference. That seems to be so for no apparent reason, nobenefit. Not only does that defeats users' expectation but it alsodoes hinder existing software (written either in C or Fortran)integration within programs written in C++. There is no reason whysubscripting an expression of type valarray<T> that is const-qualifiedshould not return a const T&.

Proposed resolution:

In the class synopsis in 29.6.2[template.valarray], and in29.6.2.4[valarray.access] just above paragraph 1, change

  T operator[](size_t const);

to

  const T& operator[](size_t const);

[Kona: fixed a minor typo: put semicolon at the end of the line wehre it belongs.]

Rationale:

Return by value seems to serve no purpose. Valaray was explicitlydesigned to have a specified layout so that it could easily beintegrated with libraries in other languages, and return by valuedefeats that purpose. It is believed that this change will have noimpact on allowable optimizations.


[8]ページ先頭

©2009-2026 Movatter.jp