Title
ranges::to can still return views
Status
new
Section
[range.utility.conv.to] [range.utility.conv.adaptors]
Submitter
Hewill Kang

Created on 2023-01-06.00:00:00 last changed 3 weeks ago

Messages

Date: 2023-01-06.16:23:59

Proposed resolution:

This wording is relative to N4917.

  1. Modify [ranges.syn] as indicated:

    #include <compare>              // see [compare.syn]
    #include <initializer_list>     // see [initializer.list.syn]
    #include <iterator>             // see [iterator.synopsis]
    
    namespace std::ranges {
      […]
      // [range.utility.conv], range conversions
      template<class C, input_range R, class... Args> requires (!view<remove_cv_t<C>>)
        constexpr C to(R&& r, Args&&... args);                                          // freestanding
      template<template<class...> class C, input_range R, class... Args>
        constexpr auto to(R&& r, Args&&... args);                                       // freestanding
      template<class C, class... Args> requires (!view<remove_cv_t<C>>)
        constexpr auto to(Args&&... args);                                              // freestanding
      template<template<class...> class C, class... Args>
        constexpr auto to(Args&&... args);                                              // freestanding
      […]
    }
    
  2. Modify [range.utility.conv.to] as indicated:

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

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

    […]
  3. Modify [range.utility.conv.adaptors] as indicated:

    template<class C, class... Args> requires (!view<remove_cv_t<C>>)
      constexpr auto to(Args&&... args);
    template<template<class...> class C, class... Args>
      constexpr auto to(Args&&... args);
    

    -1- Returns: A range adaptor closure object ([range.adaptor.object]) f that is a perfect forwarding call wrapper ([func.require]) with the following properties:

    […]
Date: 2023-01-06.00:00:00

The intention of ranges::to is to construct a non-view object, which is reflected in its constraint that object type C should not model a view.

The specification allows C to be a const-qualified type which does not satisfy view such as const string_view, making ranges::to return a dangling view. We should ban such cases.

History
Date User Action Args
2023-01-06 16:23:59adminsetmessages: + msg13181
2023-01-06 00:00:00admincreate