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

543. valarray slice default constructor

Section: 28.6.4 [class.slice] Status: CD1 Submitter: Howard Hinnant Opened: 2005-11-03 Last modified: 2016-01-28

Priority: Not Prioritized

View all issues with CD1 status.

Discussion:

If one explicitly constructs a slice or glice with the default constructor, does the standard require this slice to have any usable state? It says "creates a slice which specifies no elements", which could be interpreted two ways:

  1. There are no elements to which the slice refers (i.e. undefined).
  2. The slice specifies an array with no elements in it (i.e. defined).

Here is a bit of code to illustrate:

#include <iostream>
#include <valarray>

int main()
{
    std::valarray<int> v(10);
    std::valarray<int> v2 = v[std::slice()];
    std::cout << "v[slice()].size() = " << v2.size() << '\n';
}

Is the behavior undefined? Or should the output be:

v[slice()].size() = 0

There is a similar question and wording for gslice at 26.3.6.1p1.

Proposed resolution:

[Martin suggests removing the second sentence in 28.6.4.2 [cons.slice] as well.]

Change 28.6.4.2 [cons.slice]:

1 - The default constructor for slice creates a slice which specifies no elements. The default constructor is equivalent to slice(0, 0, 0). A default constructor is provided only to permit the declaration of arrays of slices. The constructor with arguments for a slice takes a start, length, and stride parameter.

Change 28.6.6.2 [gslice.cons]:

1 - The default constructor creates a gslice which specifies no elements. The default constructor is equivalent to gslice(0, valarray<size_t>(), valarray<size_t>()). The constructor with arguments builds a gslice based on a specification of start, lengths, and strides, as explained in the previous section.