名前空間
変種
操作

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

From 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-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` に変換可能なすべてのものを挿入することを許可した。 隠しフレンドにした。
English 日本語 中文(简体) 中文(繁體)