<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2676: Provide filesystem::path overloads for File-based streams</title>
<meta property="og:title" content="Issue 2676: Provide filesystem::path overloads for File-based streams">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2676.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++17">C++17</a> status.</em></p>
<h3 id="2676"><a href="lwg-defects.html#2676">2676</a>. Provide <code>filesystem::path</code> overloads for <i>File-based streams</i></h3>
<p><b>Section:</b> 31.10 <a href="https://wg21.link/file.streams">[file.streams]</a> <b>Status:</b> <a href="lwg-active.html#C++17">C++17</a>
 <b>Submitter:</b> Beman Dawes <b>Opened:</b> 2016-03-16 <b>Last modified:</b> 2017-07-30</p>
<p><b>Priority: </b>2
</p>
<p><b>View all other</b> <a href="lwg-index.html#file.streams">issues</a> in [file.streams].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++17">C++17</a> status.</p>
<p><b>Discussion:</b></p>
  <p>
    The constructor and <code>open</code> functions for <i>File-based streams</i> in
    27.9 [file.streams] currently provide overloads for <code>const char*</code> and
    <code>const string&amp;</code> arguments that specify the path for the file to be opened.
    With the addition of the File System TS to the standard library, these
    constructors and <code>open</code> functions need to be overloaded for <code>
      const filesystem::path&amp;
    </code> so that file-based streams can take advantage of class <code>filesystem::path</code>
    features such as support for strings of character types <code>wchar_t</code>,
    <code>char16_t</code>, and <code>char32_t</code>.
  </p>
  <p>
    The <code>
      const filesystem::path&amp; p
    </code> overload for these functions is like the
    existing <code>const string&amp;</code> overload; it simply calls the overload of
    the same function that takes a C-style string.</p>
  <p> For operating systems like POSIX
    that traffic in <code>char</code> strings for filenames, nothing more is
    required. For operating systems like Windows that traffic in <code>wchar_t</code>
    strings for filenames, an additional C-style string overload is required.
    The overload's character type needs to be specified as
    <code>std::filesystem::path::value_type</code> to also support possible future
    operating systems that traffic in <code>char16_t</code> or <code>char32_t</code> characters.
  </p>
  
  <p><b>Not recommended:</b></p>
  <p>Given the proposed constructor and <code>open</code> signatures taking
  <code>const filesystem::path&amp;</code>, it would in theory be possible to remove 
  some of the other signatures. This is not proposed because it would break ABI's, break
  user code depending on user-defined automatic conversions to the existing argument types,
  and invalidate existing documentation, books, and tutorials.</p>
  
  <p><b>Implementation experience:</b></p>
  <p>
    The Boost Filesystem library has provided header <code>&lt;boost/filesystem/fstream.hpp&gt;</code>
    implementing the proposed resolution for over a decade.
  </p>
  <p>
    The Microsoft/Dinkumware
    implementation of standard library header <code>&lt;fstream&gt;</code> has provided
    the <code>const wchar_t*</code>
    overloads for many years.
  </p>

<p><i>[2016-08-03 Chicago]</i></p>

<p>Fri PM: Move to Review</p>


<p id="res-2676"><b>Proposed resolution:</b></p>

<p><i><span style="background-color: #E1E9FD">At the end of 27.9.1 File streams 
[</span><span style="background-color: #E1E9FD">fstreams</span><span style="background-color: #E1E9FD">] 
add a paragraph:</span></i></p>

<p>In this subclause, member functions taking arguments of <code>
const std::filesystem::path::value_type*</code> shall only be provided on systems 
where <code>std::filesystem::path::value_type</code> ([fs.class.path]) is not <code>
char</code>. [<i>Note:</i> These functions enable class <code>path</code> 
support for systems with a wide native path character type, such as <code>
wchar_t</code>. &mdash; <i>end note</i>]</p>

<p><i><span style="background-color: #E1E9FD">Change 27.9.1.1 Class template 
basic_filebuf [filebuf] as indicated:</span></i></p>

<pre>basic_filebuf&lt;charT,traits&gt;* open(const char* s,
    ios_base::openmode mode);
<ins>basic_filebuf&lt;charT,traits&gt;* open(const std::filesystem::path::value_type* s,</ins>
    <ins>ios_base::openmode mode);  // wide systems only; see [fstreams]</ins> 
basic_filebuf&lt;charT,traits&gt;* open(const string&amp; s,
   ios_base::openmode mode);
<ins>basic_filebuf&lt;charT,traits&gt;* open(const filesystem::path&amp; p,</ins>
   <ins>ios_base::openmode mode);</ins></pre>
   
<p><i><span style="background-color: #E1E9FD">Change 27.9.1.4 Member functions [filebuf.members] 
as indicated:</span></i></p>

<pre>basic_filebuf&lt;charT,traits&gt;* open(const char* s,
   ios_base::openmode mode);
<ins>basic_filebuf&lt;charT,traits&gt;* open(const std::filesystem::path::value_type* s,</ins>
   <ins>ios_base::openmode mode);  // wide systems only; see [fstreams]</ins></pre>
   
<p><i><span style="background-color: #E1E9FD">To 27.9.1.4 Member functions [filebuf.members] 
add:</span></i></p>

<pre><ins>basic_filebuf&lt;charT,traits&gt;* open(const filesystem::path&amp; p,</ins>
   <ins>ios_base::openmode mode);</ins></pre>
   
<blockquote>
  <p><i>Returns:</i> <code>open(p.c_str(), mode);</code></p>
</blockquote>

  <p><i><span style="background-color: #E1E9FD">Change 27.9.1.6 Class template 
basic_ifstream [ifstream] as indicated:</span></i></p>

<pre>explicit basic_ifstream(const char* s,
    ios_base::openmode mode = ios_base::in);
<ins>explicit basic_ifstream(const std::filesystem::path::value_type* s,</ins>
    <ins>ios_base::openmode mode = ios_base::in);  // wide systems only; see [fstreams]</ins>
explicit basic_ifstream(const string&amp; s,
    ios_base::openmode mode = ios_base::in);
<ins>explicit basic_ifstream(const filesystem::path&amp; p,</ins>
    <ins>ios_base::openmode mode = ios_base::in);</ins>
...
void open(const char* s,
    ios_base::openmode mode = ios_base::in);
<ins>void open(const std::filesystem::path::value_type* s,</ins>
    <ins>ios_base::openmode mode = ios_base::in);  // wide systems only; see [fstreams]</ins>
void open(const string&amp; s,
    ios_base::openmode mode = ios_base::in);
<ins>void open(const filesystem::path&amp; p,</ins>
    <ins>ios_base::openmode mode = ios_base::in);</ins></pre>

<p><i><span style="background-color: #E1E9FD">Change 27.9.1.7 basic_ifstream 
constructors [ifstream.cons] as indicated:</span></i></p>

<pre>explicit basic_ifstream(const char* s,
    ios_base::openmode mode = ios_base::in);
<ins>explicit basic_ifstream(const std::filesystem::path::value_type* s,</ins>
    <ins>ios_base::openmode mode = ios_base::in);  // wide systems only; see [fstreams]</ins></pre>

<p><i><span style="background-color: #E1E9FD">To 27.9.1.7 basic_ifstream 
constructors [ifstream.cons] 
add:</span></i></p>
<pre><ins>explicit basic_ifstream(const filesystem::path&amp; p,</ins>
    <ins>ios_base::openmode mode = ios_base::in);</ins></pre>
   <blockquote>
  <p><ins><i>Effects: </i>the same as <code>basic_ifstream(p.c_str(), mode)</code>.</ins></p>
</blockquote>
   
<p><i><span style="background-color: #E1E9FD">Change 27.9.1.9 Member functions [ifstream.members] 
as indicated:</span></i></p>
   
<pre>void open(const char* s,
    ios_base::openmode mode = ios_base::in);
<ins>void open(const std::filesystem::path::value_type* s,</ins>
    <ins>ios_base::openmode mode = ios_base::in);  // wide systems only; see [fstreams]</ins></pre>
   
<p><i><span style="background-color: #E1E9FD">To 27.9.1.9 Member functions [ifstream.members] 
add:</span></i></p>
<pre><ins>void open(const filesystem::path&amp; p,</ins>
    <ins>ios_base::openmode mode = ios_base::in);</ins></pre>
   <blockquote>
  <p><ins><i>Effects:</i> calls <code>open(p.c_str(), mode)</code>.</ins></p>
</blockquote>

<p><i><span style="background-color: #E1E9FD">Change 27.9.1.10 Class template 
basic_ofstream [ofstream] as indicated:</span></i></p>

<pre>explicit basic_ofstream(const char* s,
    ios_base::openmode mode = ios_base::out);
<ins>explicit basic_ofstream(const std::filesystem::path::value_type* s,</ins>
    <ins>ios_base::openmode mode = ios_base::out);  // wide systems only; see [fstreams]</ins>
explicit basic_ofstream(const string&amp; s,
    ios_base::openmode mode = ios_base::out);
<ins>explicit basic_ofstream(const filesystem::path&amp; p,</ins>
    <ins>ios_base::openmode mode = ios_base::out);</ins>
...
void open(const char* s,
    ios_base::openmode mode = ios_base::out);
<ins>void open(const std::filesystem::path::value_type* s,</ins>
    <ins>ios_base::openmode mode = ios_base::out);  // wide systems only; see [fstreams]</ins>
void open(const string&amp; s,
    ios_base::openmode mode = ios_base::out);
<ins>void open(const filesystem::path&amp; p,</ins>
    <ins>ios_base::openmode mode = ios_base::out);</ins></pre>

<p><i><span style="background-color: #E1E9FD">Change 27.9.1.11 basic_ofstream 
constructors [ofstream.cons] as indicated:</span></i></p>

<pre>explicit basic_ofstream(const char* s,
    ios_base::openmode mode = ios_base::out);
<ins>explicit basic_ofstream(const std::filesystem::path::value_type* s,</ins>
    <ins>ios_base::openmode mode = ios_base::out);  // wide systems only; see [fstreams]</ins></pre>

<p><i><span style="background-color: #E1E9FD">To 27.9.1.11 basic_ofstream 
constructors [ofstream.cons] 
add:</span></i></p>
<pre><ins>explicit basic_ofstream(const filesystem::path&amp; p,</ins>
    <ins>ios_base::openmode mode = ios_base::out);</ins></pre>
   <blockquote>
  <p><ins><i>Effects: </i>the same as <code>basic_ofstream(p.c_str(), mode)</code>.</ins></p>
</blockquote>
   
<p><i><span style="background-color: #E1E9FD">Change 27.9.1.13 Member functions [ofstream.members] 
as indicated:</span></i></p>
   
<pre>void open(const char* s,
    ios_base::openmode mode = ios_base::out);
<ins>void open(const std::filesystem::path::value_type* s,</ins>
    <ins>ios_base::openmode mode = ios_base::out);  // wide systems only; see [fstreams]</ins></pre>
   
<p><i><span style="background-color: #E1E9FD">To 27.9.1.13 Member functions [ofstream.members] 
add:</span></i></p>
<pre><ins>void open(const filesystem::path&amp; p,</ins>
    <ins>ios_base::openmode mode = ios_base::out);</ins></pre>
   <blockquote>
  <p><ins><i>Effects:</i> calls <code>open(p.c_str(), mode)</code>.</ins></p>
</blockquote>

<p><i><span style="background-color: #E1E9FD">Change 27.9.1.14 Class template 
basic_fstream [fstream] as indicated:</span></i></p>

<pre>explicit basic_fstream(const char* s,
    ios_base::openmode mode = ios_base::in|ios_base::out);
<ins>explicit basic_fstream(const std::filesystem::path::value_type* s,</ins>
    <ins>ios_base::openmode mode = ios_base::in|ios_base::out);  // wide systems only; see [fstreams]</ins>
explicit basic_fstream(const string&amp; s,
    ios_base::openmode mode = ios_base::in|ios_base::out);
<ins>explicit basic_fstream(const filesystem::path&amp; p,</ins>
    <ins>ios_base::openmode mode = ios_base::in|ios_base::out);</ins>
...
void open(const char* s,
    ios_base::openmode mode = ios_base::in|ios_base::out);
<ins>void open(const std::filesystem::path::value_type* s,</ins>
    <ins>ios_base::openmode mode = ios_base::in|ios_base::out);  // wide systems only; see [fstreams]</ins>
void open(const string&amp; s,
    ios_base::openmode mode = ios_base::in|ios_base::out);
<ins>void open(const filesystem::path&amp; p,</ins>
    <ins>ios_base::openmode mode = ios_base::in|ios_base::out);</ins></pre>

<p><i><span style="background-color: #E1E9FD">Change 27.9.1.15 basic_fstream 
constructors [fstream.cons] as indicated:</span></i></p>

<pre>explicit basic_fstream(const char* s,
    ios_base::openmode mode = ios_base::in|ios_base::out);
<ins>explicit basic_fstream(const std::filesystem::path::value_type* s,</ins>
    <ins>ios_base::openmode mode = ios_base::in|ios_base::out);  // wide systems only; see [fstreams]</ins></pre>

<p><i><span style="background-color: #E1E9FD">To 27.9.1.15 basic_fstream 
constructors [fstream.cons] 
add:</span></i></p>
<pre><ins>explicit basic_fstream(const filesystem::path&amp; p,</ins>
    <ins>ios_base::openmode mode = ios_base::in|ios_base::out);</ins></pre>
   <blockquote>
  <p><ins><i>Effects: </i>the same as <code>basic_fstream(p.c_str(), mode)</code>.</ins></p>
</blockquote>
   
<p><i><span style="background-color: #E1E9FD">Change 27.9.1.17 Member functions [fstream.members] 
as indicated:</span></i></p>
   
<pre>void open(const char* s,
    ios_base::openmode mode = ios_base::in|ios_base::out);
<ins>void open(const std::filesystem::path::value_type* s,</ins>
    <ins>ios_base::openmode mode = ios_base::in|ios_base::out);  // wide systems only; see [fstreams]</ins></pre>
   
<p><i><span style="background-color: #E1E9FD">To 27.9.1.17 Member functions [fstream.members] 
add:</span></i></p>
<pre><ins>void open(const filesystem::path&amp; p,</ins>
    <ins>ios_base::openmode mode = ios_base::in|ios_base::out);</ins></pre>
   <blockquote>
  <p><ins><i>Effects:</i> calls <code>open(p.c_str(), mode)</code>.</ins></p>
</blockquote>






</body>
</html>
