forked from lwg/issues
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New issue from Hewill Kang: "projected<I, identity> should just be I"
- Loading branch information
Showing
1 changed file
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?xml version='1.0' encoding='utf-8' standalone='no'?> | ||
<!DOCTYPE issue SYSTEM "lwg-issue.dtd"> | ||
|
||
<issue num="3996" status="New"> | ||
<title><tt>projected<I, identity></tt> should just be <tt>I</tt></title> | ||
<section><sref ref="[projected]"/></section> | ||
<submitter>Hewill Kang</submitter> | ||
<date>12 Oct 2023</date> | ||
<priority>99</priority> | ||
|
||
<discussion> | ||
<p> | ||
Currently, <tt>projected</tt> is a wrapper of the implementation type regardless of whether <tt>Proj</tt> is <tt>identity</tt>. | ||
<p/> | ||
Since <tt>identity</tt> always returns a reference, this prevents <tt>projected<I, identity></tt> from fully | ||
emulating the properties of the original iterator when its reference is a prvalue. | ||
<p/> | ||
Such non-equivalence may lead to unexpected behavior in some cases (<a href="https://godbolt.org/z/KM45ndWvh">demo</a>): | ||
</p> | ||
<blockquote><pre> | ||
#include <algorithm> | ||
#include <ranges> | ||
#include <iostream> | ||
|
||
int main() { | ||
auto outer = std::views::iota(0, 5) | ||
| std::views::transform([](int i) { | ||
return std::views::single(i) | std::views::filter([](int) { return true; }); | ||
}); | ||
|
||
for (auto&& inner : outer) | ||
for (auto&& elem : inner) | ||
std::cout << elem << " "; // 0 1 2 3 4 | ||
|
||
std::ranges::for_each( | ||
outer, | ||
[](auto&& inner) { | ||
// <span style="color:#C80000;font-weight:bold">error: passing 'const filter_view' as 'this' argument discards qualifiers</span> | ||
for (auto&& elem : inner) | ||
std::cout << elem << " "; | ||
}); | ||
} | ||
</pre></blockquote> | ||
<p> | ||
In the above example, <tt>ranges::for_each</tt> requires <tt>indirect_unary_predicate<Pred, projected<I, identity>></tt> | ||
which ultimately requires <tt>invocable<Pred&, iter_common_reference_t<projected<I, identity>>></tt>. | ||
<p/> | ||
According to the current wording, the reference and value type of <tt>projected<I, identity></tt> are <tt>filter_view&&</tt> | ||
and <tt>filter_view&</tt> respectively, which causes its common reference to be eventually calculated as | ||
<tt>const filter_view&</tt>. Since the former is not <tt>const</tt>-iterable, this results in a hard error during | ||
instantiation because <tt>const begin</tt> is called unexpectedly in an unconstrained lambda. | ||
</p> | ||
<p> | ||
It seems like having <tt>projected<I, identity></tt> just be <tt>I</tt> is a more appropriate choice, | ||
which makes the concept checking really specific to <tt>I</tt> rather than a potentially incomplete iterator wrapper. | ||
</p> | ||
</discussion> | ||
|
||
<resolution> | ||
<p> | ||
This wording is relative to <paper num="N4958"/>. | ||
</p> | ||
|
||
<ol> | ||
|
||
<li><p>Modify <sref ref="[projected]"/> as indicated:</p> | ||
|
||
<blockquote> | ||
<p> | ||
-1- Class template <tt>projected</tt> is used to constrain algorithms that accept callable objects and projections | ||
(<sref ref="[defns.projection]"/>). It combines an <tt>indirectly_readable</tt> type <tt>I</tt> and a callable | ||
object type <tt>Proj</tt> into a new <tt>indirectly_readable</tt> type whose <tt>reference</tt> type is the | ||
result of applying <tt>Proj</tt> to the <tt>iter_reference_t</tt> of <tt>I</tt>. | ||
</p> | ||
<blockquote><pre> | ||
namespace std { | ||
template<class I, class Proj> | ||
struct <i>projected-impl</i> { // <i>exposition only</i> | ||
struct <i>type</i> { // <i>exposition only</i> | ||
using value_type = remove_cvref_t<indirect_result_t<Proj&, I>>; | ||
using difference_type = iter_difference_t<I>; // <i>present only if</i> I | ||
// <i>models</i> weakly_incrementable | ||
indirect_result_t<Proj&, I> operator*() const; // <i>not defined</i> | ||
}; | ||
}; | ||
|
||
template<indirectly_readable I, indirectly_regular_unary_invocable<I> Proj> | ||
using projected = <ins>conditional_t<is_same_v<Proj, identity>, I, typename</ins> <i>projected-impl</i><I, Proj>::type<ins>></ins>; | ||
} | ||
</pre></blockquote> | ||
</blockquote> | ||
|
||
|
||
</li> | ||
|
||
</ol> | ||
</resolution> | ||
|
||
</issue> |