命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | filesystem‎ | path
 
 
 
 
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;
}

[编辑] 示例

#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++ 标准。

DR 应用于 已发布行为 正确行为
LWG 2989 C++17 在存在 using-directive 的情况下,允许插入任何可转换为 path 的内容 设为隐藏友元