Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::filesystem::path::lexically_normal,std::filesystem::path::lexically_relative,std::filesystem::path::lexically_proximate

      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 lexically_normal()const;
      (1)(since C++17)
      path lexically_relative(const path& base)const;
      (2)(since C++17)
      path lexically_proximate(const path& base)const;
      (3)(since C++17)
      1) Returns*this converted tonormal form in its generic format.
      2) Returns*this made relative tobase.
      • First, ifroot_name()!= base.root_name() istrue oris_absolute()!= base.is_absolute() istrue or(!has_root_directory()&& base.has_root_directory()) istrue or any filename inrelative_path() orbase.relative_path() can be interpreted as aroot-name, returns a default-constructed path.
      • Otherwise, first determines the first mismatched element of*this andbase as if byauto[a, b]= mismatch(begin(), end(), base.begin(), base.end()), then
      • ifa== end() andb== base.end(), returnspath("."),
      • otherwise, defineN as the number of nonempty filename elements that are neitherdot nordot-dot in[b, base.end()), minus the number ofdot-dot filename elements, IfN < 0, returns a default-constructed path,
      • otherwise, ifN = 0 anda== end()|| a->empty(), returnspath("."),
      • otherwise returns an object composed from
      • a default-constructedpath() followed by
      • N applications ofoperator/=(path("..")), followed by
      • one application ofoperator/= for each element in the half-open range[aend()).
      3) If the value oflexically_relative(base) is not an empty path, return it. Otherwise return*this.

      Contents

      [edit]Parameters

      (none)

      [edit]Return value

      1) The normal form of the path.
      2) The relative form of the path.
      3) The proximate form of the path.

      [edit]Exceptions

      May throw implementation-defined exceptions.

      [edit]Notes

      These conversions are purely lexical. They do not check that the paths exist, do not follow symlinks, and do not access the filesystem at all. For symlink-following counterparts oflexically_relative andlexically_proximate, seerelative andproximate.

      On Windows, the returnedpath has backslashes (the preferred separators).

      On POSIX, no filename in a relative path is acceptable as aroot-name.

      [edit]Example

      Run this code
      #include <cassert>#include <filesystem>#include <iostream>namespace fs= std::filesystem; int main(){assert(fs::path("a/./b/..").lexically_normal()=="a/");assert(fs::path("a/.///b/../").lexically_normal()=="a/");assert(fs::path("/a/d").lexically_relative("/a/b/c")=="../../d");assert(fs::path("/a/b/c").lexically_relative("/a/d")=="../b/c");assert(fs::path("a/b/c").lexically_relative("a")=="b/c");assert(fs::path("a/b/c").lexically_relative("a/b/c/x/y")=="../..");assert(fs::path("a/b/c").lexically_relative("a/b/c")==".");assert(fs::path("a/b").lexically_relative("c/d")=="../../a/b");assert(fs::path("a/b").lexically_relative("/a/b")=="");assert(fs::path("a/b").lexically_proximate("/a/b")=="a/b");}

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3070C++17a filename that can also be a root-name may cause surprising resulttreated as error case
      LWG 3096C++17trailing "/" and "/." are handled incorrectlycorrected

      [edit]See also

      composes a relative path
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/filesystem/path/lexically_normal&oldid=158038"

      [8]ページ先頭

      ©2009-2026 Movatter.jp