• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    function
    <filesystem>

    std::filesystem::path::lexically_proximate

    path lexically_proximate(const path& base) const;
    

    概要

    文字列レベルで相対パスに変換する。

    この関数は、*thisが保持するパス文字列を、パスbaseからの相対パスに変換する。lexically_relative()メンバ関数と違い、相対パスの解決に失敗した場合に、空のパスの代わりに*thisのパスが返る。

    ファイルシステムを介した相対パスへの変換を行う場合は、std::filesystem::proximate()関数を使用すること。

    戻り値

    lexically_relative(base)の結果が空のパスでなければそれを返し、空のパスが返された場合は*thisを返す。

    備考

    • この関数は、*thisbaseのどちらに対してもパスの正規化を行わない。必要であれば、どちらか、もしくは両方にlexically_normal()メンバ関数を適用すること

    #include <cassert>
    #include <filesystem>
    
    namespace fs = std::filesystem;
    
    int main()
    {
      assert(fs::path("/a/d").lexically_proximate("/a/b/c") == "../../d");
      assert(fs::path("/a/b/c").lexically_proximate("/a/d") == "../b/c");
      assert(fs::path("a/b/c").lexically_proximate("a") == "b/c");
      assert(fs::path("a/b/c").lexically_proximate("a/b/c/x/y") == "../..");
      assert(fs::path("a/b/c").lexically_proximate("a/b/c") == ".");
      assert(fs::path("a/b").lexically_proximate("c/d") == "../../a/b");
    
      // 相対パスの解決ができなかった場合
      assert(fs::path("d").lexically_proximate("/a/b/c") == "d");
    }
    

    出力

    バージョン

    言語

    • C++17

    処理系