std::filesystem::operator/(std::filesystem::path)
来自 cppreference.cn
< cpp | filesystem | path
friend path operator/( const path& lhs, const path& rhs ); |
(自 C++17 起) | |
如果合适,使用首选目录分隔符连接两个路径组件(详情请参阅 operator/=)。
有效返回 path(lhs) /= rhs。
此函数对于普通的非限定或限定查找是不可见的,并且只有当 std::filesystem::path 是参数的关联类时,才能通过实参依赖查找找到。这防止了在使用 using namespace std::filesystem; using-directive 时出现不希望的转换。
目录 |
[编辑] 参数
lhs, rhs | - | 要连接的路径 |
[编辑] 返回值
路径连接的结果。
[编辑] 示例
运行此代码
#include <filesystem> #include <iostream> int main() { # if defined(_WIN32) // see e.g. stackoverflow.com/questions/142508 std::filesystem::path p = "C:"; std::cout << "\"C:\" / \"Users\" / \"batman\" == " << p / "Users" / "batman" << '\n'; # else // __linux__ etc std::filesystem::path p = "/home"; std::cout << "\"/home\" / \"tux\" / \".fonts\" == " << p / "tux" / ".fonts" << '\n'; # endif }
可能的输出
Windows specific output: "C:" / "Users" / "batman" == "C:Users\\batman" Linux etc specific output: "/home" / "tux" / ".fonts" == "/home/tux/.fonts"
[编辑] 缺陷报告
以下行为变更缺陷报告已追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 3065 | C++17 | 允许在使用 using-directive 的情况下连接任何可转换为 path 的内容 | 设为隐藏友元 |
[编辑] 参见
将元素附加到带有目录分隔符的路径 (公共成员函数) |