|
| 1 | +<?xml version='1.0' encoding='utf-8' standalone='no'?> |
| 2 | +<!DOCTYPE issue SYSTEM "lwg-issue.dtd"> |
| 3 | + |
| 4 | +<issue num="4125" status="New"> |
| 5 | +<title><tt>move_iterator</tt>'s default constructor should be constrained</title> |
| 6 | +<section><sref ref="[move.iterator]"/><sref ref="[move.iter.cons]"/></section> |
| 7 | +<submitter>Hewill Kang</submitter> |
| 8 | +<date>22 Jul 2024</date> |
| 9 | +<priority>99</priority> |
| 10 | + |
| 11 | +<discussion> |
| 12 | +<p> |
| 13 | +Although it is unclear why <paper num="P2325"/> did not apply to <tt>move_iterator</tt>, there is |
| 14 | +implementation divergence among the current libraries (<a href="https://godbolt.org/z/WdoPcjjv4">demo</a>): |
| 15 | +</p> |
| 16 | +<blockquote><pre> |
| 17 | +#include <istream> |
| 18 | +#include <iterator> |
| 19 | +#include <ranges> |
| 20 | + |
| 21 | +using R = std::ranges::istream_view<int>; |
| 22 | +using I = std::ranges::iterator_t<R>; |
| 23 | +using MI = std::move_iterator<I>; |
| 24 | +static_assert(std::default_initializable<MI>); // <span style="color:#C80000;font-weight:bold">libstdc++ passes, libc++ fails</span> |
| 25 | +</pre></blockquote> |
| 26 | +<p> |
| 27 | +As libc++ additionally requires that its default constructors satisfy <code>is_constructible_v<Iterator></code>. |
| 28 | +<p/> |
| 29 | +Although this is not current standard-conforming behavior, such constraint does make sense since <code>move_iterator</code> |
| 30 | +only requires the underlying iterator to be <code>input_iterator</code> which may not be <code>default_initializable</code>. |
| 31 | +</p> |
| 32 | +</discussion> |
| 33 | + |
| 34 | +<resolution> |
| 35 | +<p> |
| 36 | +This wording is relative to <paper num="N4986"/>. |
| 37 | +</p> |
| 38 | + |
| 39 | +<ol> |
| 40 | +<li><p>Modify <sref ref="[move.iterator]"/> as indicated:</p> |
| 41 | + |
| 42 | +<blockquote> |
| 43 | +<pre> |
| 44 | +namespace std { |
| 45 | + template<class Iterator> |
| 46 | + class move_iterator { |
| 47 | + public: |
| 48 | + […] |
| 49 | + constexpr move_iterator() <ins>requires default_initializable<Iterator> = default</ins>; |
| 50 | + […] |
| 51 | + private: |
| 52 | + Iterator current <ins>= Iterator()</ins>; // <i>exposition only</i> |
| 53 | + }; |
| 54 | +} |
| 55 | +</pre> |
| 56 | +</blockquote> |
| 57 | + |
| 58 | +</li> |
| 59 | + |
| 60 | +<li><p>Modify <sref ref="[move.iter.cons]"/> as indicated:</p> |
| 61 | + |
| 62 | +<blockquote> |
| 63 | +<pre> |
| 64 | +<del>constexpr move_iterator();</del> |
| 65 | +</pre> |
| 66 | +<blockquote> |
| 67 | +<p> |
| 68 | +<del>-1- <i>Effects</i>: Value-initializes <tt>current</tt>.</del> |
| 69 | +</p> |
| 70 | +</blockquote> |
| 71 | +</blockquote> |
| 72 | + |
| 73 | +</li> |
| 74 | +</ol> |
| 75 | +</resolution> |
| 76 | + |
| 77 | +</issue> |
0 commit comments