Title
movable-box as member should use default-initialization instead of copy-initialization
Status
c++23
Section
[range.repeat.view][range.chunk.by.view]
Submitter
Hewill Kang

Created on 2022-10-20.00:00:00 last changed 13 months ago

Messages

Date: 2022-11-17.00:42:33

Proposed resolution:

This wording is relative to N4917.

  1. Modify [range.repeat.view] as indicated:

    namespace std::ranges {
      template<move_constructible W, semiregular Bound = unreachable_sentinel_t>
        requires (is_object_v<W> && same_as<W, remove_cv_t<W>> &&
                  (is-integer-like<Bound> || same_as<Bound, unreachable_sentinel_t>))
       class repeat_view : public view_interface<repeat_view<W, Bound>> {
       private:
         // [range.repeat.iterator], class repeat_view::iterator
         struct iterator;                            // exposition only
    
         movable-box<W> value_ = W();                // exposition only, see [range.move.wrap]
         Bound bound_ = Bound();                     // exposition only
         […]
       };
       […]
    }
    
  2. Modify [range.chunk.by.view] as indicated:

    namespace std::ranges {
      template<forward_range V, indirect_binary_predicate<iterator_t<V>, iterator_t<V>> Pred>
        requires view<V> && is_object_v<Pred>
      class chunk_by_view : public view_interface<chunk_by_view<V, Pred>> {
        V base_ = V();                                          // exposition only
        movable-box<Pred> pred_ = Pred();                       // exposition only
    
        // [range.chunk.by.iter], class chunk_by_view::iterator
        class iterator;                                         // exposition only
        […]
      };
      […]
    }
    
Date: 2022-11-12.00:00:00

[ 2022-11-12 Approved at November 2022 meeting in Kona. Status changed: Voting → WP. ]

Date: 2022-11-15.00:00:00

[ 2022-11-01; Reflector poll ]

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

Date: 2022-10-20.00:00:00

Currently, the member variable value_ of repeat_view is initialized with the following expression:

movable-box<W> value_ = W();

which will result in the following unexpected error in libstdc++:

#include <ranges>

int main() {
  std::ranges::repeat_view<int> r; // error: could not convert '0' from 'int' to 'std::ranges::__detail::__box<int>'
}

This is because the single-argument constructors of the simple version of movable-box in libstdc++ are declared as explicit, which is different from the conditional explicit declared by the optional's constructor, resulting in no visible conversion between those two types.

For MSVC-STL, the simple version of movable-box does not provide a single-argument constructor, so this form of initialization will also produce a hard error.

This may be a bug of existing implementations, but we don't need such "copy-initialization", the default-constructed movable-box already value-initializes the underlying type.

Simply using default initialization, as most other range adaptors do, guarantees consistency, which also eliminates extra move construction.

History
Date User Action Args
2023-11-22 15:47:43adminsetstatus: wp -> c++23
2022-11-17 00:42:33adminsetmessages: + msg13084
2022-11-17 00:42:33adminsetstatus: voting -> wp
2022-11-08 03:46:49adminsetstatus: ready -> voting
2022-11-01 17:49:23adminsetmessages: + msg12912
2022-11-01 17:49:23adminsetstatus: new -> ready
2022-10-22 15:36:31adminsetmessages: + msg12883
2022-10-20 00:00:00admincreate