Namespaces
Variants

std::basic_ios<CharT,Traits>:: copyfmt

From cppreference.net
basic_ios & copyfmt ( const basic_ios & other ) ;

만약 other * this 와 동일한 객체를 참조하면, 아무런 효과도 없습니다. 그렇지 않으면, 스트림 other 의 상태를 * this 로 복사합니다. 이는 다음 순서로 수행됩니다:

1) register_callback() 로 등록된 모든 콜백을 호출하며, erase_event 를 매개변수로 전달합니다.
2) other 의 모든 멤버 객체들을 * this 로 복사합니다. 단, rdstate() , 예외 마스크, 그리고 rdbuf() 는 제외됩니다. 특히 로케일, 서식 플래그, std::ios_base::iword std::ios_base::pword 배열의 내용들(단, iword pword 포인터 자체는 제외), 콜백들, 그리고 연결된 스트림의 복사본을 만듭니다.
3) register_callback() 를 통해 등록된 모든 콜백을 호출하며, copyfmt_event 를 매개변수로 전달합니다.
4) other 에서 예외 마스크를 * this 로 복사합니다. 마치 exceptions ( other. exceptions ( ) ) 를 호출하는 것처럼 동작합니다.

목차

매개변수

other - 다른 소스로 사용할 스트림

반환값

* this

참고 사항

콜백을 통한 두 번째 패스는 std::ios_base::pword 내 포인터들이 가리키는 사용자 정의 객체들을 딥 카피하는 데 사용될 수 있습니다.

copyfmt() 는 스트림의 상태를 저장하고 복원하는 데 사용될 수 있습니다. Boost는 동일한 목적으로 더 세분화된 I/O 상태 저장기 라이브러리를 제공합니다.

예제

std::ofstream 객체 "out"이 서식 지정, tie() 등을 포함하여 std::cout 과 완전히 동일하게 동작하도록 만듭니다.

#include <bitset>
#include <climits>
#include <fstream>
#include <iostream>
int main()
{
    std::ofstream out;
    out.copyfmt(std::cout); // copy everything except rdstate and rdbuf
    out.clear(std::cout.rdstate()); // copy rdstate
    out.basic_ios<char>::rdbuf(std::cout.rdbuf()); // share the buffer
    out << "Hello, world\n";
    auto bin = [](std::ios_base::fmtflags f)
    {
        return std::bitset<sizeof(std::ios_base::fmtflags) * CHAR_BIT>
            { static_cast<unsigned long long>(f) };
    };
    std::ofstream out2;
    std::cout << "1) out2.flags(): " << bin(out2.flags()) << '\n';
    std::cout << "2) cout.flags(): " << bin(std::cout.flags()) << '\n';
    std::cout.setf(std::ios::hex | std::ios::fixed | std::ios::boolalpha);
    std::cout << "3) cout.flags(): " << bin(std::cout.flags()) << '\n';
    out2.copyfmt(std::cout); // copy everything except rdstate and rdbuf
    std::cout << "4) out2.flags(): " << bin(out2.flags()) << '\n';
}

가능한 출력:

Hello, world
1) out2.flags(): 00000000000000000001000000000010
2) cout.flags(): 00000000000000000001000000000010
3) cout.flags(): 00000000000000000001000000001111
4) out2.flags(): 00000000000000000001000000001111

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 256 C++98 3단계에서 등록된 콜백을
정의되지 않은 이벤트 타입 copy_event 로 호출함
다음으로 수정됨
copyfmt_event
LWG 292 C++98 other * this 와 동일한 객체를 참조할 경우,
멤버 객체들이 여전히 복사되고 등록된 콜백들이 여전히 호출됨
이 경우
아무 작업도 수행하지 않음