Title
ranges::to's copy branch is underconstrained
Status
new
Section
[range.utility.conv.to]
Submitter
Hewill Kang

Created on 2023-11-25.00:00:00 last changed 9 months ago

Messages

Date: 2024-03-11.21:55:38

Proposed resolution:

This wording is relative to N4964.

  1. Modify [range.utility.conv.to] as indicated:

    template<class C, input_range R, class... Args> requires (!view<C>)
      constexpr C to(R&& r, Args&&... args);
    

    -1- Mandates: C is a cv-unqualified class type.

    -2- Returns: An object of type C constructed from the elements of r in the following manner:

    1. (2.1) — If C does not satisfy input_range or convertible_to<range_reference_t<R>, range_value_t<C>> is true:

      1. (2.1.1) — […]

      2. (2.1.2) — […]

      3. (2.1.3) — […]

      4. (2.1.4) — Otherwise, if

        1. (2.1.4.?) — move_constructible<C> is true,

        2. (2.1.4.1) — constructible_from<C, Args...> is true, and

        3. (2.1.4.2) — container-insertable<C, range_reference_t<R>> is true:

          C c(std::forward<Args>(args)...);
          if constexpr (sized_range<R> && reservable-container<C>)
            c.reserve(static_cast<range_size_t<C>>(ranges::size(r)));
          ranges::copy(r, container-inserter<range_reference_t<R>>(c));
          
      5. (2.1.5) — Otherwise, the program is ill-formed.

    2. (2.2) — […]

    3. (2.3) — Otherwise, the program is ill-formed.

Date: 2024-03-15.00:00:00

[ 2024-03-11; Reflector poll ]

Set priority to 3 after reflector poll.

Date: 2023-11-25.00:00:00

Unlike other branches that return a prvalue C, this branch's C is returned by elidable move, indicating that C needs to be move constructible (demo):

#include <ranges>
#include <vector>

struct nonmovable {
  nonmovable() = default;
  nonmovable(const nonmovable&) = delete;
  nonmovable& operator=(const nonmovable&) = delete;
};

template<class T>
struct nonmovable_vector : std::vector<T>, nonmovable { };

int main() {
  int arr[] = {42};
  auto v = std::ranges::to<nonmovable_vector<int>>(arr); // hard error
}
History
Date User Action Args
2024-03-11 21:55:38adminsetmessages: + msg13985
2023-11-25 19:07:03adminsetmessages: + msg13876
2023-11-25 00:00:00admincreate