Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::filesystem::path::path

      From cppreference.com
      <cpp‎ |filesystem‎ |path
       
       
      Filesystem library
      Classes
      Functions
      File types
       
      std::filesystem::path
      Member types
      Member constants
      Member functions
      Path decomposition
      Non-member functions
      (until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
      Helper classes
       
      path()noexcept;
      (1)(since C++17)
      path(const path& p);
      (2)(since C++17)
      path( path&& p)noexcept;
      (3)(since C++17)
      path( string_type&& source, format fmt= auto_format);
      (4)(since C++17)
      template<class Source>
      path(const Source& source, format fmt= auto_format);
      (5)(since C++17)
      template<class InputIt>
      path( InputIt first, InputIt last, format fmt= auto_format);
      (6)(since C++17)
      template<class Source>
      path(const Source& source,conststd::locale& loc, format fmt= auto_format);
      (7)(since C++17)
      template<class InputIt>
      path( InputIt first, InputIt last,conststd::locale& loc, format fmt= auto_format);
      (8)(since C++17)

      Constructs a newpath object.

      1) Constructs an empty path.
      2) Copy constructor. Constructs a path whose pathname, in both native and generic formats, is the same as that ofp.
      3) Move constructor. Constructs a path whose pathname, in both native and generic formats, is the same as that ofp,p is left in valid but unspecified state.
      4-6) Constructs the path from a character sequence (format interpreted as specified byfmt) provided bysource(4,5), which is a pointer or an input iterator to a null-terminated character/wide character sequence, anstd::basic_string or anstd::basic_string_view, or represented as a pair of input iterators [first,last)(6). Any of the character typeschar,char8_t,(since C++20)char16_t,char32_t,wchar_t is allowed, and the method of conversion to the native character set depends on the character type used bysource.
      • If the source character type ischar, the encoding of the source is assumed to be the native narrow encoding (so no conversion takes place on POSIX systems).
      • If the source character type ischar8_t, conversion from UTF-8 to native filesystem encoding is used.
      (since C++20)
      • If the source character type ischar16_t, conversion from UTF-16 to native filesystem encoding is used.
      • If the source character type ischar32_t, conversion from UTF-32 to native filesystem encoding is used.
      • If the source character type iswchar_t, the input is assumed to be the native wide encoding (so no conversion takes places on Windows).
      7,8) Constructs the path from a character sequence (format interpreted as specified byfmt) provided bysource(7), which is a pointer or an input iterator to a null-terminated character sequence, anstd::string, anstd::string_view, or represented as a pair of input iterators [first,last)(8). The only character type allowed ischar. Usesloc to perform the character encoding conversion. Ifvalue_type iswchar_t, converts from to wide using thestd::codecvt<wchar_t,char,std::mbstate_t> facet ofloc. Otherwise, first converts to wide using thestd::codecvt<wchar_t,char,std::mbstate_t> facet and then converts to filesystem native character type usingstd::codecvt<wchar_t,value_type> facet ofloc.

      (5) and(7) participate in overload resolution only ifSource andpath are not the same type, and either:

      Contents

      [edit]Parameters

      p - a path to copy
      source -std::basic_string,std::basic_string_view, pointer to a null-terminated character string, or input iterator with a character value type that points to a null-terminated character sequence (the character type must bechar for overload(7))
      first, last - pair ofLegacyInputIterators that specify a character sequence
      fmt - enumerator of typepath::format which specifies how pathname format is to be interpreted
      loc - locale that defines encoding conversion to use
      Type requirements
      -
      InputIt must meet the requirements ofLegacyInputIterator.
      -
      The value type ofInputIt must be one of the character typeschar,wchar_t,char8_t,(since C++20)char16_t andchar32_t to use the overload(6).
      -
      The value type ofInputIt must bechar to use the overload(8).

      [edit]Exceptions

      2,4-8) May throw implementation-defined exceptions.

      [edit]Notes

      For portable pathname generation from Unicode strings, seeu8path.

      (until C++20)

      path constructor supports creation from UTF-8 string when the source is a sequence ofchar8_t.

      (since C++20)

      [edit]Example

      Run this code
      #include <filesystem>#include <iostream>namespace fs= std::filesystem; int main(){    fs::path p1="/usr/lib/sendmail.cf";// portable format    fs::path p2="C:\\users\\abcdef\\AppData\\Local\\Temp\\";// native format    fs::path p3= U"D:/猫.txt";// UTF-32 string    fs::path p4= u8"~/狗.txt";// UTF-8 string std::cout<<"p1 = "<< p1<<'\n'<<"p2 = "<< p2<<'\n'<<"p3 = "<< p3<<'\n'<<"p4 = "<< p4<<'\n';}

      Output:

      p1 = "/usr/lib/sendmail.cf"p2 = "C:\\users\\abcdef\\AppData\\Local\\Temp\\"p3 = "D:/猫.txt"p4 = "~/狗.txt"

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3244C++17constraint thatSource cannot bepath was missingadded

      [edit]See also

      (C++17)(deprecated in C++20)
      creates apath from a UTF-8 encoded source
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/filesystem/path/path&oldid=160182"

      [8]ページ先頭

      ©2009-2025 Movatter.jp