Title
path's stream insertion operator lets you insert everything under the sun
Status
c++20
Section
[fs.path.io]
Submitter
Billy O'Neal III

Created on 2017-06-27.00:00:00 last changed 46 months ago

Messages

Date: 2018-03-18.16:03:30

Proposed resolution:

This resolution is relative to N4659.

  1. Edit [fs.filesystem.syn], header <filesystem> synopsis, as indicated:

    // [fs.path.io], path inserter and extractor
    template <class charT, class traits>
      basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const path& p);
    template <class charT, class traits>
      basic_istream<charT, traits>&
        operator>>(basic_istream<charT, traits>& is, path& p);
    
  2. Edit [fs.path.io] as indicated:

    [Drafting note: The project editor is kindly asked to consider to move sub-clause [fs.path.io] before sub-clause [fs.path.nonmember] (as a peer of it) — end drafting note]

    template <class charT, class traits>
      friend basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& os, const path& p);
    
    […]
    
    template <class charT, class traits>
      friend basic_istream<charT, traits>&
        operator>>(basic_istream<charT, traits>& is, path& p);
    
  3. Edit [fs.class.path] p2, class path synopsis, as indicated:

    namespace std::filesystem {
      class path {
      public:
        […]
        iterator begin() const;
        iterator end() const;
    
        // [fs.path.io] path inserter and extractor
        template <class charT, class traits>
          friend basic_ostream<charT, traits>&
            operator<<(basic_ostream<charT, traits>& os, const path& p);
        template <class charT, class traits>
          friend basic_istream<charT, traits>&
            operator>>(basic_istream<charT, traits>& is, path& p);
      };
    }
    
Date: 2018-03-17.00:00:00

[ 2018-3-17 Adopted in Jacksonville ]

Date: 2018-01-26.00:00:00

[ 2018-1-26 issues processing telecon ]

Status to 'Tentatively Ready'

Date: 2017-07-16.06:17:26

[ 2017-07 Toronto Saturday afternoon ]

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

Date: 2017-07-12.01:35:02

[ 2017-07 Toronto Monday issue prioritization ]

Priority 2

Date: 2017-06-27.00:00:00

The rules for converting a path 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.

#include <iostream>
#include <filesystem>
#include <string>

void foo() {
  using namespace std;
  using namespace std::experimental::filesystem::v1;
  wstring val(L"abc");
  std::cout << val;
}

Using the godbolt online compiler we get:

foo PROC
  sub      rsp, 104   ; 00000068H
  lea      rdx, OFFSET FLAT:$SG44895
  lea      rcx, QWORD PTR val$[rsp]
  call     std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>
           >::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >
  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
  call     std::experimental::filesystem::v1::operator<<<char,std::char_traits<char> >
  lea      rcx, QWORD PTR $T1[rsp]
  call     std::experimental::filesystem::v1::path::~path
  lea      rcx, QWORD PTR val$[rsp]
  call     std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>
           >::~basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >
  add      rsp, 104   ; 00000068H
  ret      0
foo ENDP

This should either be disabled with a SFINAE constraint, use the auto_ptr user-defined conversion trick, or the stream insertion operators should be made "hidden friends" to prevent conversions to path from being considered here.

History
Date User Action Args
2021-02-25 10:48:01adminsetstatus: wp -> c++20
2018-03-18 16:03:30adminsetmessages: + msg9732
2018-03-18 16:03:30adminsetstatus: voting -> wp
2018-02-12 01:13:49adminsetstatus: ready -> voting
2018-01-28 19:43:07adminsetmessages: + msg9654
2018-01-28 19:43:07adminsetstatus: new -> ready
2017-07-16 06:17:26adminsetmessages: + msg9402
2017-07-16 06:17:26adminsetmessages: + msg9401
2017-07-12 01:35:02adminsetmessages: + msg9351
2017-06-27 00:00:00admincreate