표준 (N3337
)는 (27.5.3.1.1 Class ios_base::failure
)를 말한다 :C++ 표준 : : ios_base :: 실패 예외가
나는 표준 : : ostringstream의 사용하는 동안 제한된 자원 환경을 에뮬레이트하는 간단한 테스트 프로그램이 있습니다. One of online compilers은 동일한 출력을 보여줍니다.클래스 실패가 IOSTREAMS의 기능에 의해, 예외로 던져 모든 객체 의 유형에 대한 기본 클래스를 정의한다 라이브러리에서 스트림 버퍼 조작 중 오류를 감지했습니다. 내가
stderr
에Caught: std::bad_alloc
있어 내 환경 (리눅스, GCC 5.3.0)에서#include <sys/time.h> #include <sys/resource.h> #include <errno.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <sstream> int main(int argc, const char* argv[]) { rlimit limit; limit.rlim_cur = limit.rlim_max = 268435456; if(setrlimit(RLIMIT_AS, &limit)) { std::cerr << "Cannot set resource limit: " << strerror(errno) << std::endl; exit(EXIT_FAILURE); } std::ostringstream os; os.exceptions(std::ostringstream::badbit); try { auto iterations = 1024 * 1024 * 1024; while(iterations && --iterations) os << 'F'; } catch(const std::ios_base::failure& ex) { std::cerr << "Caught: std::ios_base::failure" << std::endl; } catch(const std::bad_alloc& ex) { std::cerr << "Caught: std::bad_alloc" << std::endl; } catch(...) { std::cerr << "Caught: ellipsis" << std::endl; } return 0; }
:
질문은 다음과 같습니다. 예외 유형이 std::bad_alloc
이고 std::ios_base::failure
이 아닌 이유는 무엇입니까?
하지만, 메모리 할당에 의해 (버퍼 용량은 제
bad_alloc
타격없이string::max_size
도달하면 된 libstdc에서 ++,stringbuf::overflow
여전히eof
를 반환 할 수)? –다시 캡처하여 std :: ios_base :: failure 예외로 채우지 않겠습니까? 나는 메모리 할당 자와 함께 iostream과 함께 일하고있다. – user1641854
그렇게 생각할 수도 있지만, 표준은이를 요구하지 않습니다. 'bad_alloc'이 발생 된 후에 다른 예외를 생성하기위한 공간이 남지 않을 수도 있다는 문제도 있습니다. –