This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of C++11 status.

### 430. `valarray` subset operations

Section: 26.7.2.5 [valarray.sub] Status: C++11 Submitter: Martin Sebor Opened: 2003-09-18 Last modified: 2016-01-28

Priority: Not Prioritized

View all issues with C++11 status.

Discussion:

The standard fails to specify the behavior of valarray::operator[](slice) and other valarray subset operations when they are passed an "invalid" slice object, i.e., either a slice that doesn't make sense at all (e.g., slice (0, 1, 0) or one that doesn't specify a valid subset of the valarray object (e.g., slice (2, 1, 1) for a valarray of size 1).

[Kona: the LWG believes that invalid slices should invoke undefined behavior. Valarrays are supposed to be designed for high performance, so we don't want to require specific checking. We need wording to express this decision.]

[ Bellevue: ]

> Please note that the standard also fails to specify the behavior of slice_array and gslice_array in the valid case. Bill Plauger will endeavor to provide revised wording for `slice_array` and `gslice_array`.

[ post-Bellevue: Bill provided wording. ]

[ 2009-07 Frankfurt ]

> Move to Ready.

[ 2009-11-04 Pete opens: ]

> The resolution to LWG issue 430 has not been applied — there have been changes to the underlying text, and the resolution needs to be reworked.

[ 2010-03-09 Matt updated wording. ]

[ 2010 Pittsburgh: Moved to Ready for Pittsburgh. ]

Proposed resolution:

Replace 26.7.2.5 [valarray.sub], with the following:

> The member operator is overloaded to provide several ways to select sequences of elements from among those controlled by `*this`. Each of these operations returns a subset of the array. The const-qualified versions return this subset as a new `valarray`. The non-const versions return a class template object which has reference semantics to the original array, working in conjunction with various overloads of `operator=` (and other assigning operators) to allow selective replacement (slicing) of the controlled sequence. In each case the selected element(s) must exist.

>     valarray<T> operator[](slice slicearr) const;

> > This function returns an object of class `valarray<T>` containing those elements of the controlled sequence designated by `slicearr`. [Example:

> > >     valarray<char> v0("abcdefghijklmnop", 16);
> > >     valarray<char> v1("ABCDE", 5);
> > >     v0[slice(2, 5, 3)] = v1;
> > >     // v0 == valarray<char>("abAdeBghCjkDmnEp", 16)

> > end example]

>     valarray<T> operator[](slice slicearr);

> > This function selects those elements of the controlled sequence designated by `slicearr`. [Example:

> > >     valarray<char> v0("abcdefghijklmnop", 16);
> > >     valarray<char> v1("ABCDE", 5);
> > >     v0[slice(2, 5, 3)] = v1;
> > >     // v0 == valarray<char>("abAdeBghCjkDmnEp", 16)

> > end example]

>     valarray<T> operator[](const gslice& gslicearr) const;

> > This function returns an object of class `valarray<T>` containing those elements of the controlled sequence designated by `gslicearr`. [Example:

> > >     valarray<char> v0("abcdefghijklmnop", 16);
> > >     const size_t lv[] = {2, 3};
> > >     const size_t dv[] = {7, 2};
> > >     const valarray<size_t> len(lv, 2), str(dv, 2);
> > >     // v0[gslice(3, len, str)] returns
> > >     // valarray<char>("dfhkmo", 6)

> > end example]

>     gslice_array<T> operator[](const gslice& gslicearr);

> > This function selects those elements of the controlled sequence designated by `gslicearr`. [Example:

> > >     valarray<char> v0("abcdefghijklmnop", 16);
> > >     valarray<char> v1("ABCDEF", 6);
> > >     const size_t lv[] = {2, 3};
> > >     const size_t dv[] = {7, 2};
> > >     const valarray<size_t> len(lv, 2), str(dv, 2);
> > >     v0[gslice(3, len, str)] = v1;
> > >     // v0 == valarray<char>("abcAeBgCijDlEnFp", 16)

> > end example]

>     valarray<T> operator[](const valarray<bool>& boolarr) const;

> > This function returns an object of class `valarray<T>` containing those elements of the controlled sequence designated by `boolarr`. [Example:

> > >     valarray<char> v0("abcdefghijklmnop", 16);
> > >     const bool vb[] = {false, false, true, true, false, true};
> > >     // v0[valarray<bool>(vb, 6)] returns
> > >     // valarray<char>("cdf", 3)

> > end example]

>     mask_array<T> operator[](const valarray<bool>& boolarr);

> > This function selects those elements of the controlled sequence designated by `boolarr`. [Example:

> > >     valarray<char> v0("abcdefghijklmnop", 16);
> > >     valarray<char> v1("ABC", 3);
> > >     const bool vb[] = {false, false, true, true, false, true};
> > >     v0[valarray<bool>(vb, 6)] = v1;
> > >     // v0 == valarray<char>("abABeCghijklmnop", 16)

> > end example]

>     valarray<T> operator[](const valarray<size_t>& indarr) const;

> > This function returns an object of class `valarray<T>` containing those elements of the controlled sequence designated by `indarr`. [Example:

> > >     valarray<char> v0("abcdefghijklmnop", 16);
> > >     const size_t vi[] = {7, 5, 2, 3, 8};
> > >     // v0[valarray<size_t>(vi, 5)] returns
> > >     // valarray<char>("hfcdi", 5)

> > end example]

>     indirect_array<T> operator[](const valarray<size_t>& indarr);

> > This function selects those elements of the controlled sequence designated by `indarr`. [Example:

> > >     valarray<char> v0("abcdefghijklmnop", 16);
> > >     valarray<char> v1("ABCDE", 5);
> > >     const size_t vi[] = {7, 5, 2, 3, 8};
> > >     v0[valarray<size_t>(vi, 5)] = v1;
> > >     // v0 == valarray<char>("abCDeBgAEjklmnop", 16)

> > end example]