Title
chunk_view and slide_view should not be default_initializable
Status
c++23
Section
[range.chunk.view.input][range.chunk.view.fwd][range.slide.view]
Submitter
Hewill Kang

Created on 2022-06-10.00:00:00 last changed 12 months ago

Messages

Date: 2022-07-25.20:32:58

Proposed resolution:

This wording is relative to N4910.

  1. Modify [range.chunk.view.input] as indicated:

    namespace std::ranges {
      […]
     
      template<view V>
        requires input_range<V>
      class chunk_view : public view_interface<chunk_view<V>> {
        V base_ = V();                                        // exposition only
        range_difference_t<V> n_ = 0;                         // exposition only
        range_difference_t<V> remainder_ = 0;                 // exposition only
        […]
      public:
        chunk_view() requires default_initializable<V> = default;
        constexpr explicit chunk_view(V base, range_difference_t<V> n);
        […]
      };
      […]
    }
    
  2. Modify [range.chunk.view.fwd] as indicated:

    namespace std::ranges {
      template<view V>
        requires forward_range<V>
      class chunk_view<V> : public view_interface<chunk_view<V>> {
        V base_ = V();                   // exposition only
        range_difference_t<V> n_ = 0;    // exposition only
        […]
      public:
        chunk_view() requires default_initializable<V> = default;
        constexpr explicit chunk_view(V base, range_difference_t<V> n);
    
        […]
      };
    }
    
  3. Modify [range.slide.view] as indicated:

    namespace std::ranges {
      […]
    
      template<forward_range V>
        requires view<V>
      class slide_view : public view_interface<slide_view<V>> {
        V base_ = V();                      // exposition only
        range_difference_t<V> n_ = 0;       // exposition only
        […]
      public:
        slide_view() requires default_initializable<V> = default;
        constexpr explicit slide_view(V base, range_difference_t<V> n);
    
        […]
      };
      […]
    }
    
Date: 2022-07-25.00:00:00

[ 2022-07-25 Approved at July 2022 virtual plenary. Status changed: Ready → WP. ]

Date: 2022-07-15.00:00:00

[ 2022-07-15; LWG telecon: move to Ready ]

Date: 2022-06-15.00:00:00

[ 2022-06-21; Reflector poll ]

Set status to Tentatively Ready after six votes in favour during reflector poll.

Date: 2022-06-10.00:00:00

Both chunk_view and slide_view have a precondition that N must be positive, but they are still default_initializable when the underlying range is default_initializable, which makes the member variable n_ initialized with an invalid value 0 when they are default-constructed, which produces the following unexpected result:

#include <ranges>

using V = std::ranges::iota_view<int, int>;
static_assert(std::ranges::slide_view<V>().empty()); // fails
static_assert(std::ranges::chunk_view<V>().empty()); // division by zero is not a constant expression

Although we could provide a default positive value for n_, I think a more appropriate solution would be to not provide the default constructor, since default-constructed values for integer types will never be valid.

History
Date User Action Args
2023-11-22 15:47:43adminsetstatus: wp -> c++23
2022-07-25 20:32:58adminsetmessages: + msg12653
2022-07-25 20:32:58adminsetstatus: ready -> wp
2022-07-25 20:28:19adminsetmessages: + msg12627
2022-06-21 11:47:28adminsetmessages: + msg12523
2022-06-21 11:47:28adminsetstatus: new -> ready
2022-06-11 16:18:11adminsetmessages: + msg12495
2022-06-10 00:00:00admincreate