名稱空間
變體
操作

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

來自 cppreference.com
< 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-指示時發生不期望的轉換。

目錄

[編輯] 引數

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++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 2989 C++17 在有 using-指示 的情況下,允許插入所有可轉換為 path 的內容 設為隱藏友元