std::filesystem::path:: lexically_normal, std::filesystem::path:: lexically_relative, std::filesystem::path:: lexically_proximate
From cppreference.net
<
cpp
|
filesystem
|
path
|
path lexically_normal
(
)
const
;
|
(1) | (自 C++17 起) |
|
path lexically_relative
(
const
path
&
base
)
const
;
|
(2) | (自 C++17 起) |
|
path lexically_proximate
(
const
path
&
base
)
const
;
|
(3) | (自 C++17 起) |
2)
返回相对于
base
的
*
this
。
-
- 首先,若 root_name ( ) ! = base. root_name ( ) 为 true ,或 is_absolute ( ) ! = base. is_absolute ( ) 为 true ,或 ( ! has_root_directory ( ) && base. has_root_directory ( ) ) 为 true ,或 relative_path ( ) 或 base. relative_path ( ) 中的任何文件名可被解释为 根名称 ,则返回默认构造的路径。
- 否则,首先通过 auto [ a, b ] = mismatch ( begin ( ) , end ( ) , base. begin ( ) , base. end ( ) ) 确定 * this 与 base 的第一个不匹配元素,然后
-
- 若 a == end ( ) 且 b == base. end ( ) ,返回 path ( "." ) ,
- 否则,定义 N 为 [ b, base. end ( ) ) 中既非 点 亦非 点点 的非空文件名元素数量,减去 点点 文件名元素的数量。若 N < 0 ,返回默认构造的路径,
- 否则,若 N = 0 且 a == end ( ) || a - > empty ( ) ,返回 path ( "." ) ,
- 否则返回由以下内容组成的对象:
-
- 默认构造的 path ( ) ,后接
- N 次 operator / = ( path ( ".." ) ) 的应用,后接
-
对半开区间
[a,end ( ))中的每个元素应用一次 operator / = 。
3)
若
lexically_relative
(
base
)
的返回值不是空路径,则返回该值。否则返回
*
this
。
目录 |
参数
(无)
返回值
1)
路径的标准形式。
2)
路径的相对形式。
3)
路径的近似形式。
异常
可能抛出实现定义的异常。
注释
这些转换纯粹是词法层面的。它们不会检查路径是否存在,不会跟随符号链接,且完全不会访问文件系统。关于
lexically_relative
和
lexically_proximate
的符号链接跟随版本,请参阅
relative
和
proximate
。
在 Windows 系统上,返回的
path
包含反斜杠(首选分隔符)。
在 POSIX 系统上,相对路径中的任何文件名都不能作为 root-name 使用。
示例
运行此代码
#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"); }
缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的C++标准。
| 缺陷报告 | 适用范围 | 发布时行为 | 正确行为 |
|---|---|---|---|
| LWG 3070 | C++17 | 可能同时作为根名称的文件名会导致意外结果 | 按错误情况处理 |
| LWG 3096 | C++17 | 尾部"/"和"/."处理不正确 | 已修正 |
参见
|
(C++17)
|
组合相对路径
(函数) |