Title
path construction and assignment should have "string_type&&" overloads
Status
c++17
Section
[fs.class.path]
Submitter
Eric Fiselier

Created on 2016-05-09.00:00:00 last changed 90 months ago

Messages

Date: 2017-03-19.19:35:20

Proposed resolution:

This wording is relative to N4582.

  1. Change [fs.class.path], class path synopsis, as indicated:

    [Drafting note: Making the string_type&& constructors and assignment operators noexcept would over-constrain implementations which may need to perform construct additional state]

    namespace std::filesystem {
      class path {
      public:
        […]
        // 27.10.8.4.1, constructors and destructor
        path() noexcept;
        path(const path& p);
        path(path&& p) noexcept;
        path(string_type&& source);
        template <class Source>
        path(const Source& source);
        […]
        
        // 27.10.8.4.2, assignments
        path& operator=(const path& p);
        path& operator=(path&& p) noexcept;
        path& operator=(string_type&& source);
        path& assign(string_type&& source);
        template <class Source>
        path& operator=(const Source& source);
        template <class Source>
        path& assign(const Source& source)
        template <class InputIterator>
        path& assign(InputIterator first, InputIterator last);    
        […]
      };
    }
    
  2. Add a new paragraph following [fs.path.construct]/3:

    path(string_type&& source);
    

    -?- Effects: Constructs an object of class path with pathname having the original value of source. source is left in a valid but unspecified state.

  3. Add a new paragraph following [fs.path.assign]/4:

    path& operator=(string_type&& source);
    path& assign(string_type&& source);
    

    -?- Effects: Modifies pathname to have the original value of source. source is left in a valid but unspecified state.

    -?- Returns:*this

Date: 2016-05-09.00:00:00

Currently construction of a path from the native string_type always performs a copy, even when that string is passed as a rvalue. This is a large pessimization as paths are commonly constructed from temporary strings.

One pattern I frequently see is:

path foo(path const& p) {
  auto s = p.native();
  mutateString(s);
  return s;
}

Implementations should be allowed to move from s and avoid an unnecessary allocation. I believe string_type&& constructor and assignment operator overloads should be added to support this.

History
Date User Action Args
2017-07-30 20:15:43adminsetstatus: wp -> c++17
2016-06-28 12:50:44adminsetstatus: ready -> wp
2016-05-22 15:38:38adminsetstatus: new -> ready
2016-05-10 20:14:04adminsetmessages: + msg8113
2016-05-09 00:00:00admincreate