Namespaces
Variants

std::basic_stringbuf<CharT,Traits,Allocator>:: overflow

From cppreference.net
protected :
virtual int_type overflow ( int_type c = Traits :: eof ( ) ) ;

출력 문자 시퀀스에 문자 c 를 추가합니다.

만약 c 가 파일 끝 표시자라면 ( traits :: eq_int_type ( c, traits :: eof ( ) ) == true ), 추가할 문자가 없습니다. 함수는 아무 작업도 수행하지 않고 traits :: eof ( ) 가 아닌 지정되지 않은 값을 반환합니다.

그렇지 않고, 출력 시퀀스에 쓰기 위치가 사용 가능하거나 이 함수가 쓰기 위치를 성공적으로 사용 가능하게 만들 수 있는 경우, sputc ( c ) 를 호출하고 c 를 반환합니다.

이 함수는 std::stringbuf 가 출력용으로 열려 있는 경우( ( mode & ios_base :: out ) ! = 0 ) 쓰기 위치를 사용 가능하게 만들 수 있습니다: 이 경우 전체 현재 버퍼와 최소 하나 이상의 추가 문자를 수용할 수 있을 만큼 충분히 큰 버퍼를 재할당(또는 초기 할당)합니다. 만약 std::stringbuf 가 입력용으로도 열려 있다면( ( mode & ios_base :: in ) ! = 0 ), overflow egptr() 를 새로운 쓰기 위치 바로 뒤를 가리키도록 이동시켜 읽기 영역의 크기도 증가시킵니다.

목차

매개변수

c - put 영역에 저장할 문자

반환값

Traits :: eof ( ) 는 실패를 나타내기 위해, c 는 문자가 성공적으로 추가된 경우, 또는 Traits :: eof ( ) 이외의 다른 값을 반환합니다. 만약 Traits :: eof ( ) 가 인수로 전달된 경우에도 동일하게 적용됩니다.

참고 사항

이 함수는 일반적인 overflow() 와 다릅니다. 일반적인 overflow()는 버퍼의 내용을 연관된 문자 시퀀스로 이동시키지만, std::basic_stringbuf 의 경우 버퍼와 연관된 시퀀스가 동일하기 때문입니다.


예제

이 예제를 실행하는 데 사용된 구현(예: GCC-4.9)에서, overflow() 는 put 영역을 512바이트로 초과 할당합니다: str() 호출은 초기화된 4바이트만 반환하지만, 다음 508번의 sputc() 호출은 overflow() 의 새로운 호출을 필요로 하지 않습니다.

#include <sstream>
#include <iostream>
struct mybuf : std::stringbuf
{
    mybuf(const std::string& new_str,
          std::ios_base::openmode which = std::ios_base::in | std::ios_base::out)
        : std::stringbuf(new_str, which) {}
    int_type overflow(int_type c = EOF) override
    {
        std::cout << "stringbuf::overflow('" << char(c) << "') called\n"
                  << "Before: size of get area: " << egptr() - eback() << '\n'
                  << "        size of put area: " << epptr() - pbase() << '\n';
        int_type ret = std::stringbuf::overflow(c);
        std::cout << "After : size of get area: " << egptr() - eback() << '\n'
                  << "        size of put area: " << epptr() - pbase() << '\n';
        return ret;
    }
};
int main()
{
    std::cout << "read-write stream:\n";
    mybuf sbuf("   "); // read-write stream
    std::iostream stream(&sbuf);
    stream << 1234;
    std::cout << sbuf.str() << '\n';
    std::cout << "\nread-only stream:\n";
    mybuf ro_buf("   ", std::ios_base::in); // read-only stream
    std::iostream ro_stream(&ro_buf);
    ro_stream << 1234;
    std::cout << "\nwrite-only stream:\n";
    mybuf wr_buf("   ", std::ios_base::out); // write-only stream
    std::iostream wr_stream(&wr_buf);
    wr_stream << 1234;
}

가능한 출력:

read-write stream:
stringbuf::overflow('4') called
Before: size of get area: 3
        size of put area: 3
After : size of get area: 4
        size of put area: 512
1234
read-only stream:
stringbuf::overflow('1') called
Before: size of get area: 3
        size of put area: 0
After : size of get area: 3
        size of put area: 0
write-only stream:
stringbuf::overflow('4') called
Before: size of get area: 0
        size of put area: 3
After : size of get area: 0
        size of put area: 512

결함 보고서

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

DR 적용 대상 게시된 동작 올바른 동작
LWG 169 C++98 재할당된 버퍼가 하나의 추가 문자만 보관할 수 있었음 더 많은 추가 문자를 허용함
LWG 432 C++98 overflow epptr() 를 새로운 쓰기 위치 바로 다음을 가리키도록 이동시킴
(입력 모드로 열린 std::stringbuf 인 경우)
이동되지 않음

참고 항목

[virtual]
put 영역에서 연관된 출력 시퀀스로 문자를 기록함
( std::basic_streambuf<CharT,Traits> 의 virtual protected member function)
[virtual]
입력 시퀀스에서 사용 가능한 다음 문자를 반환함
(virtual protected member function)