Namespaces
Variants

std:: swap (std::basic_ofstream)

From cppreference.net

template < class CharT, class Traits >
void swap ( basic_ofstream < CharT, Traits > & lhs, basic_ofstream < CharT, Traits > & rhs ) ;

std::swap 알고리즘을 std:: basic_ofstream 에 대해 특수화합니다. lhs 의 상태와 rhs 의 상태를 교환합니다. 효과적으로 lhs. swap ( rhs ) 를 호출합니다.

목차

매개변수

lhs, rhs - 상태를 교환할 스트림들

반환값

(없음)

예외

구현 정의 예외를 던질 수 있습니다.

예제

#include <fstream>
#include <iostream>
#include <string>
bool create_stream(std::ofstream& fs)
{
    try
    {
        std::string some_name = "/tmp/test_file.txt";
        std::ios_base::openmode some_flags = fs.trunc; // | other flags
        if (std::ofstream ts{some_name, some_flags}; ts.is_open())
        {
            std::swap(ts, fs); // stream objects are not copyable => swap
            return true;
        }
    {
    catch (...)
    {
        std::cout << "Exception!\n";
    }
    return false;
}
int main()
{
    if (std::ofstream fs; create_stream(fs))
    {
        // use fs stream
    }
}

참고 항목

(C++11)
두 파일 스트림을 교환합니다
(public member function)