Movatterモバイル変換


[0]ホーム

URL:



This page is a snapshot from the LWG issues list, see theLibrary Active Issues List for more information and the meaning ofC++23 status.

3494. Allow ranges to be conditionally borrowed

Section: 25.7.21[range.reverse], 25.7.10[range.take], 25.7.12[range.drop], 25.7.13[range.drop.while], 25.7.20[range.common], 25.7.13[range.drop.while], 25.7.23[range.elements]Status:C++23Submitter: Barry RevzinOpened: 2020-11-01Last modified: 2023-11-22

Priority:Not Prioritized

View all otherissues in [range.reverse].

View all issues withC++23 status.

Discussion:

Consider the following approach to trimming astd::string:

auto trim(std::string const& s) {  auto isalpha = [](unsigned char c){ return std::isalpha(c); };  auto b = ranges::find_if(s, isalpha);  auto e = ranges::find_if(s | views::reverse, isalpha).base();  return subrange(b, e);}

This is a fairly nice and, importantly, safe way to implementtrim. The iteratorsbande returned fromfind_if will not dangle, since they point into thestring swhose lifetime outlives the function. But the status quo in C++20 is thats | views::reverseis not a borrowed range (becausereverse_view<V> is never a borrowed range for anyV).As a result,find_if(s | views::reverse, isalpha) returnsdangling rather thana real iterator.

Instead, you have to write it this way, introducing a new named variable for the reversed view:

auto trim(std::string const& s) {  auto isalpha = [](unsigned char c){ return std::isalpha(c); };  auto b = ranges::find_if(s, isalpha);  auto reversed = s | views::reverse;  auto e = ranges::find_if(reversed, isalpha).base();  return subrange(b, e);}

But borrowed range can be a transitive property.s itself is a borrowed range (as alllvalue references are) sos | views::reverse could be made to be too, which would allowthe first example above to work with really no downside. We know such an iterator would not dangle,we just need to teach the library this.

P2017R1 resolves this by makingreverse_view<V>a borrowed range whenV is a borrowed range (and likewise several other range adapters).

[2021-01-15; Telecon prioritization]

Set status to Tentatively Ready after five P0 votes in reflector discussion.

[2021-02-26 Approved at February 2021 virtual plenary. Status changed: Tentatively Ready → WP.]

Rationale:

Resolved byP2017R1.

Proposed resolution:


[8]ページ先頭

©2009-2025 Movatter.jp