Namespaces
Variants

operator<<,>> (std::filesystem::path)

From cppreference.net
template < class CharT, class Traits >

friend std:: basic_ostream < CharT,Traits > &

operator << ( std:: basic_ostream < CharT,Traits > & os, const path & p ) ;
(1) (C++17 起)
template < class CharT, class Traits >

friend std:: basic_istream < CharT,Traits > &

operator >> ( std:: basic_istream < CharT,Traits > & is, path & p ) ;
(2) (C++17 起)

对路径 p 执行流输入或输出操作。使用 std:: quoted 可以确保在后续通过流输入运算符读取时,空格不会导致截断。

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

目录

参数

os - 执行输出操作的流
is - 执行输入操作的流
p - 要插入或提取的路径

返回值

1) os
2) is

异常

可能抛出实现定义的异常。

可能的实现

operator<<
template<class CharT, class Traits>
friend std::basic_ostream<CharT,Traits>&
    operator<<(std::basic_ostream<CharT,Traits>& os, const path& p)
{
    os << std::quoted(p.string<CharT,Traits>());
    return os;
}
operator>>
template<class CharT, class Traits>
friend std::basic_istream<CharT,Traits>&
    operator>>(std::basic_istream<CharT,Traits>& is, path& p)
{
    std::basic_string<CharT, Traits> t;
    is >> std::quoted(t);
    p = t;
    return is;
}
说明:根据要求,仅翻译了表格标题中的链接文本(operator<< 和 operator>>),保留了所有HTML标签、属性及 标签内的C++代码内容未作翻译。

示例

#include <filesystem>
#include <iostream>
int main()
{
    std::cout << std::filesystem::current_path() << '\n';
    std::cout << std::filesystem::temp_directory_path() << '\n';
}

可能的输出:

"/home/user"
"/tmp"

缺陷报告

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

缺陷报告 适用范围 发布时行为 正确行为
LWG 2989 C++17 在使用 using指令 时允许插入所有可转换为 path 类型的对象 改为隐藏友元