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-directive using namespace std::filesystem; 时,阻止不期待的转换。
目录 |
[编辑] 参数
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++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 3065 | C++17 | 在有 using-directive 时,允许连接所有可转换为 path 的东西 |
设为隐藏友元 |
[编辑] 参阅
用目录分隔符向路径追加元素 (公开成员函数) |