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 が使用されるため、後でストリーム入力演算子によって読み込まれる際にスペースによる切り捨てが発生しません。

これらの関数テンプレートは通常の unqualified lookup qualified lookup では見えず、引数に関連付けられたクラスとしてstd::filesystem::pathがある場合にのみ argument-dependent lookup によって発見されます。これは 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 に変換可能なすべての挿入を許可していた 非公開フレンド関数に変更