path lexically_proximate(const path& base) const;
概要
文字列レベルで相対パスに変換する。
この関数は、*this
が保持するパス文字列を、パスbase
からの相対パスに変換する。lexically_relative()
メンバ関数と違い、相対パスの解決に失敗した場合に、空のパスの代わりに*this
のパスが返る。
ファイルシステムを介した相対パスへの変換を行う場合は、std::filesystem::proximate()
関数を使用すること。
戻り値
lexically_relative(base)
の結果が空のパスでなければそれを返し、空のパスが返された場合は*this
を返す。
備考
- この関数は、
*this
とbase
のどちらに対してもパスの正規化を行わない。必要であれば、どちらか、もしくは両方に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
処理系
- Clang:
- GCC: 8.1 ✅
- Visual C++: 2017 Update 7 ✅