Namespaces
Variants

std::filesystem:: operator/ (std::filesystem::path)

From cppreference.net
friend path operator / ( const path & lhs, const path & rhs ) ;
(C++17 起)

在适当情况下使用首选目录分隔符连接两个路径组件(详见 operator/= )。

等效于返回 path ( lhs ) / = rhs

此函数对普通的 非限定查找 限定查找 不可见,仅当 std::filesystem::path 作为参数的关联类时,才能通过 实参依赖查找 找到。这可以防止在存在 using namespace std :: filesystem ; using 指令 时发生不期望的类型转换。

目录

参数

lhs, rhs - 要连接的路径

返回值

路径拼接的结果。

示例

#include <filesystem>
#include <iostream>
int main()
{
#   if defined(_WIN32) // 参见例如 stackoverflow.com/questions/142508
    std::filesystem::path p = "C:";
    std::cout << R"("C:\" / "Users" / "batman" == )" << p / "Users" / "batman" << '\n';
#   else // __linux__ 等系统
    std::filesystem::path p = "/home";
    std::cout << R"("/home" / "tux" / ".fonts" ==)" << p / "tux" / ".fonts" << '\n';
#   endif
}

可能的输出:

Windows 特定输出:
"C:" / "Users" / "batman" == "C:Users\\batman"
Linux 等系统特定输出:
"/home" / "tux" / ".fonts" == "/home/tux/.fonts"

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 应用于 发布时的行为 正确行为
LWG 3065 C++17 在使用 using-directive 时允许拼接所有可转换为 path 的对象 改为隐藏友元

参见

使用目录分隔符向路径追加元素
(公开成员函数)