<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2989: path's stream insertion operator lets you insert everything under the sun</title>
<meta property="og:title" content="Issue 2989: path's stream insertion operator lets you insert everything under the sun">
<meta property="og:description" content="C++ library issue. Status: C++20">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2989.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++20">C++20</a> status.</em></p>
<h3 id="2989"><a href="lwg-defects.html#2989">2989</a>. <code>path</code>'s stream insertion operator lets you insert everything under the sun</h3>
<p><b>Section:</b> 31.12.6.7 <a href="https://wg21.link/fs.path.io">[fs.path.io]</a> <b>Status:</b> <a href="lwg-active.html#C++20">C++20</a>
 <b>Submitter:</b> Billy O'Neal III <b>Opened:</b> 2017-06-27 <b>Last modified:</b> 2021-02-25</p>
<p><b>Priority: </b>2
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++20">C++20</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The rules for converting a <code>path</code> to a narrow character sequence aren't necessarily the same as that iostreams should use. Note that this program creates a temporary path and stream inserts that, which likely destroys information.
</p>

<blockquote><pre>
#include &lt;iostream&gt;
#include &lt;filesystem&gt;
#include &lt;string&gt;

void foo() {
  using namespace std;
  using namespace std::experimental::filesystem::v1;
  wstring val(L"abc");
  std::cout &lt;&lt; val;
}
</pre></blockquote>
<p>
Using <a href="https://godbolt.org/g/dTQ8Pw">the godbolt online compiler</a> we get:
</p>
<blockquote><pre>
foo PROC
  sub      rsp, 104   ; 00000068H
  lea      rdx, OFFSET FLAT:$SG44895
  lea      rcx, QWORD PTR val$[rsp]
  call     std::basic_string&lt;wchar_t,std::char_traits&lt;wchar_t&gt;,std::allocator&lt;wchar_t&gt;
           &gt;::basic_string&lt;wchar_t,std::char_traits&lt;wchar_t&gt;,std::allocator&lt;wchar_t&gt; &gt;
  lea      rdx, QWORD PTR val$[rsp]
  lea      rcx, QWORD PTR $T1[rsp]
  call     ??$?0_WU?$char_traits@_W@std@@V?$allocator@_W@1@@path@v1@filesystem@experimental@std@@QEAA@AEBV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@4@@Z
  lea      rdx, QWORD PTR $T1[rsp]
  lea      rcx, OFFSET FLAT:std::cout
  <span style="color:#C80000;font-weight:bold">call     std::experimental::filesystem::v1::operator&lt;&lt;&lt;char,std::char_traits&lt;char&gt; &gt;</span>
  lea      rcx, QWORD PTR $T1[rsp]
  <span style="color:#C80000;font-weight:bold">call     std::experimental::filesystem::v1::path::~path</span>
  lea      rcx, QWORD PTR val$[rsp]
  call     std::basic_string&lt;wchar_t,std::char_traits&lt;wchar_t&gt;,std::allocator&lt;wchar_t&gt;
           &gt;::~basic_string&lt;wchar_t,std::char_traits&lt;wchar_t&gt;,std::allocator&lt;wchar_t&gt; &gt;
  add      rsp, 104   ; 00000068H
  ret      0
foo ENDP
</pre></blockquote>
<p>
This should either be disabled with a SFINAE constraint, use the <code>auto_ptr</code> user-defined conversion
trick, or the stream insertion operators should be made "hidden friends" to prevent conversions to <code>path</code>
from being considered here.
</p>

<p><i>[2017-07 Toronto Monday issue prioritization]</i></p>

<p>Priority 2</p>

<p><i>[2017-07 Toronto Saturday afternoon]</i></p>

<p>LWG confirmed they want the hidden friend solution, Billy O'Neal to provide wording.</p>

<p><i>[2018-1-26 issues processing telecon]</i></p>

<p>Status to 'Tentatively Ready'</p>
<p><i>[2018-3-17 Adopted in Jacksonville]</i></p>



<p id="res-2989"><b>Proposed resolution:</b></p>
<p>This resolution is relative to <a href="https://wg21.link/n4659">N4659</a>.</p>

<ol>
<li><p>Edit 31.12.4 <a href="https://wg21.link/fs.filesystem.syn">[fs.filesystem.syn]</a>, header <code>&lt;filesystem&gt;</code> synopsis, as indicated:</p>

<blockquote><pre>
<del><i>// 31.12.6.7 <a href="https://wg21.link/fs.path.io">[fs.path.io]</a>,</i> path <i>inserter and extractor</i>
template &lt;class charT, class traits&gt;
  basic_ostream&lt;charT, traits&gt;&amp;
    operator&lt;&lt;(basic_ostream&lt;charT, traits&gt;&amp; os, const path&amp; p);
template &lt;class charT, class traits&gt;
  basic_istream&lt;charT, traits&gt;&amp;
    operator&gt;&gt;(basic_istream&lt;charT, traits&gt;&amp; is, path&amp; p);</del>
</pre></blockquote></li>

<li><p>Edit 31.12.6.7 <a href="https://wg21.link/fs.path.io">[fs.path.io]</a> as indicated:</p>

<blockquote class="note">
<p>
[<i>Drafting note:</i> The project editor is kindly asked to consider to move sub-clause 31.12.6.7 <a href="https://wg21.link/fs.path.io">[fs.path.io]</a> 
<em>before</em> sub-clause 31.12.6.8 <a href="https://wg21.link/fs.path.nonmember">[fs.path.nonmember]</a> (as a peer of it) &mdash; <i>end drafting note</i>]
</p>
</blockquote>

<blockquote><pre>
template &lt;class charT, class traits&gt;
  <ins>friend</ins> basic_ostream&lt;charT, traits&gt;&amp;
    operator&lt;&lt;(basic_ostream&lt;charT, traits&gt;&amp; os, const path&amp; p);

[&hellip;]

template &lt;class charT, class traits&gt;
  <ins>friend</ins> basic_istream&lt;charT, traits&gt;&amp;
    operator&gt;&gt;(basic_istream&lt;charT, traits&gt;&amp; is, path&amp; p);
</pre></blockquote></li>

<li><p>Edit 31.12.6 <a href="https://wg21.link/fs.class.path">[fs.class.path]</a> p2, class <code>path</code> synopsis, as indicated:</p>

<blockquote><pre>
namespace std::filesystem {
  class path {
  public:
    [&hellip;]
    iterator begin() const;
    iterator end() const;

    <ins><i>// 31.12.6.7 <a href="https://wg21.link/fs.path.io">[fs.path.io]</a></i> path <i>inserter and extractor</i>
    template &lt;class charT, class traits&gt;
      friend basic_ostream&lt;charT, traits&gt;&amp;
        operator&lt;&lt;(basic_ostream&lt;charT, traits&gt;&amp; os, const path&amp; p);
    template &lt;class charT, class traits&gt;
      friend basic_istream&lt;charT, traits&gt;&amp;
        operator&gt;&gt;(basic_istream&lt;charT, traits&gt;&amp; is, path&amp; p);</ins>
  };
}
</pre></blockquote></li>
</ol>




</body>
</html>
