<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2779: [networking.ts] Relax requirements on buffer sequence iterators</title>
<meta property="og:title" content="Issue 2779: [networking.ts] Relax requirements on buffer sequence iterators">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2779.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#C++23">C++23</a> status.</em></p>
<h3 id="2779"><a href="lwg-defects.html#2779">2779</a>. [networking.ts] Relax requirements on buffer sequence iterators</h3>
<p><b>Section:</b> 16.2.1 [networking.ts::buffer.reqmts.mutablebuffersequence] <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Vinnie Falco <b>Opened:</b> 2016-10-05 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++23">C++23</a> status.</p>
<p><b>Discussion:</b></p>
<p><b>Addresses: networking.ts</b></p>
<p>
We propose to relax the <b>ForwardIterator</b> requirements of buffer sequences
in [networking.ts] by allowing buffer sequence iterators to return rvalues when
dereferenced, and skip providing <code>operator-&gt;</code>.
A paraphrased explanation of why the referential equality rules of <b>ForwardIterator</b>
are harmful to the buffer sequence requirements comes from <a href="https://wg21.link/n4128">N4128</a>,
3.3.7 "Ranges For The Standard Library":
</p>
<blockquote><p>
The [networking.ts] dependence on <b>ForwardIterator</b> in the buffer
sequence requirements ties together the traversal and access properties of
iterators. For instance, no forward iterator may return an <i>rvalue</i> proxy
when it is dereferenced; the <b>ForwardIterator</b> concept requires that
unary <code>operator*</code> return an lvalue. This problem has serious consequences
for lazy evaluation that applies transformations to buffer sequence elements on
the fly. If the transformation function does not return an <i>lvalue</i>, the
range's iterator can model no concept stronger than <b>InputIterator</b>, even
if the resulting iterator could in theory support
<b>BidirectionalIterator</b>. The result in practice is that most range
adaptors today will not be compatible with [networking.ts], thereby limiting the
types that [networking.ts] can be passed, for no good reason.
</p></blockquote>

<p>
Consider a user defined function <code>trim</code> which lazily adapts a
<code>ConstBufferSequence</code>, such that when iterating the buffers in the new
sequence, each buffer appears one byte shorter than in the underlying sequence:
</p>

<blockquote><pre>
#include &lt;boost/range/adaptor/transformed.hpp&gt;

struct trim
{
  using result_type = const_buffer;
  result_type operator()(const_buffer b)
  {
    return const_buffer{b.data(), b.size() - 1};
  }
};

template &lt;ConstBufferSequence&gt;
auto
trim(ConstBufferSequence const&amp; buffers)
{
  using namespace boost::adaptors;
  return buffers | transformed(trim{});
}
</pre></blockquote>

<p>
<code>trim</code> returns a <b>BidirectionalRange</b>, whose
<code>const_iterator</code> returns an rvalue when dereferenced. This breaks the
requirements of <b>ForwardIterator</b>. A solution that meets the referential equality 
rules of <b>ForwardIterator</b>, would be to evaluate the transformed
sequence upon construction (for example, by storing each transformed
<code>const_buffer</code> in a <code>vector</code>). Unfortunately this work-around is
more expensive since it would add heap allocation which the original example avoids.
</p>

<p>
The requirement of <b>InputIterator</b> <code>operator-&gt;</code> is also
unnecessary for buffer sequence iterators, and should be removed. Because
[networking.ts] only requires that a buffer sequence iterator's
<code>value_type</code> be convertible to <code>const_buffer</code> or
<code>mutable_buffer</code>, implementations of [networking.ts] cannot assume the
existence of any particular member functions or data members other than an
implicit conversion to <code>const_buffer</code> or <code>mutable_buffer</code>.
Removing the requirement for <code>operator-&gt;</code> to be present, provides
additional relief from the referential equality requirements of
<b>ForwardIterator</b> and allows transformations of buffer sequences to meet
the requirements of buffer sequences.
</p>

<p>
This proposal imposes no changes on existing implementations of [networking.ts].
It does not change anything in the standard. The proposal is precise, minimal,
and allows range adapters to transform buffer sequences in optimized, compatible
ways.
</p>

<p><i>[Issues processing Telecon 2016-10-07]</i></p>

<p>Status set to LEWG</p>

<p><i>[2017-02-21, Jonathan comments]</i></p>

<p>
The use of the term "strict aliasing" in the issue discussion is
misleading as that refers to type-based alias analysis in compilers,
but the rule for <code>ForwardIterator</code>s is related to referential equality
and not strict aliasing.
</p>

<p><i>[2017-02-22, Vinnie Falco comments]</i></p>

<p>
We have eliminated the use of the term "strict aliasing" from the discussion.
</p>

<p><i>[2017-07-10, Toronto, LEWG comments]</i></p>

<p>Status change: LEWG &rarr; Open.</p>
<p>
Forward to LWG with the note that they may want to use "input +"
instead of "bidirectional -". Unanimous yes.
</p>
<p><i>[2017-07 Toronto Wednesday night issue processing]</i></p>

<p>Status to Ready</p>


<p id="res-2779"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/n4588">N4588</a>.
</p>

<ol>
<li>
<p>
Modify 16.2.1 [networking.ts::buffer.reqmts.mutablebuffersequence] as indicated:
</p>
<blockquote>
<p>
<del>An iterator type meeting the requirements for bidirectional
iterators (C++Std [bidirectional.iterators]) whose value type is
convertible to <code>mutable_buffer</code></del>
</p>
<p><ins>
An iterator type whose <code>reference</code> type is convertible to
<code>mutable_buffer</code>, and which satisfies all the requirements for
bidirectional iterators (C++Std [bidirectional.iterators]) except
that:
</ins></p>
<ol style="list-style-type:lower-alpha">
<li>
<ins>there is no requirement that <code>operator-&gt;</code> is provided, and</ins>
</li>
<li>
<ins>there is no requirement that <code>reference</code> be a reference type.</ins>
</li>
</ol>
</blockquote>
</li>

<li>
<p>
Modify 16.2.2 [networking.ts::buffer.reqmts.constbuffersequence] as indicated:
</p>
<blockquote>
<p>
<del>An iterator type meeting the requirements for bidirectional
iterators (C++Std [bidirectional.iterators]) whose value type is
convertible to <code>const_buffer</code>.</del>
</p>

<p>
<ins>An iterator type whose <code>reference</code> type is convertible to
<code>const_buffer</code>, and which satisfies all the requirements for
bidirectional iterators (C++Std [bidirectional.iterators]) except
that:
</ins></p>
<ol style="list-style-type:lower-alpha">
<li>
<ins>there is no requirement that <code>operator-&gt;</code> is provided, and</ins>
</li>
<li>
<ins>there is no requirement that <code>reference</code> be a reference type.</ins>
</li>
</ol>
</blockquote>
</li>
</ol>







</body>
</html>
